本文整理匯總了Java中javax.ws.rs.client.Client類的典型用法代碼示例。如果您正苦於以下問題:Java Client類的具體用法?Java Client怎麽用?Java Client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Client類屬於javax.ws.rs.client包,在下文中一共展示了Client類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getClientBuilder
import javax.ws.rs.client.Client; //導入依賴的package包/類
/**
* Gets a client web resource builder.
*
* @param localUrl the URL to access remote resource
* @return web resource builder
*/
public Invocation.Builder getClientBuilder(String localUrl) {
log.info("URL: {}", localUrl);
Client client = ClientBuilder.newClient();
WebTarget wt = client.target(localUrl);
return wt.request(UTF_8);
}
示例2: registerUser
import javax.ws.rs.client.Client; //導入依賴的package包/類
private static void registerUser(String url, MediaType mediaType) {
System.out.println("Registering user via " + url);
User user = new User(1L, "larrypage");
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Response response = target.request().post(Entity.entity(user, mediaType));
try {
if (response.getStatus() != 200) {
throw new RuntimeException("Failed with HTTP error code : " + response.getStatus());
}
System.out.println("Successfully got result: " + response.readEntity(String.class));
} finally {
response.close();
client.close();
}
}
示例3: dumpSwitches
import javax.ws.rs.client.Client; //導入依賴的package包/類
/**
* Returns Switches through Topology-Engine-Rest service.
*
* @return The JSON document of all flows
*/
public static List<SwitchInfoData> dumpSwitches() throws Exception {
System.out.println("\n==> Topology-Engine Dump Switches");
Client client = ClientBuilder.newClient(new ClientConfig());
Response response = client
.target(topologyEndpoint)
.path("/api/v1/topology/switches")
.request()
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
System.out.println(String.format("===> Response = %s", response.toString()));
List<SwitchInfoData> switches = new ObjectMapper().readValue(
response.readEntity(String.class), new TypeReference<List<SwitchInfoData>>() {});
System.out.println(String.format("====> Data = %s", switches));
return switches;
}
示例4: executeAction
import javax.ws.rs.client.Client; //導入依賴的package包/類
private Result executeAction(Action action, Object lraInfo, String lraUri) {
log.infof("executing action - %s", action);
Client client = ClientBuilder.newClient();
URI build = UriBuilder
.fromUri(servicesLocator.getServiceUri(action.getService()))
.path(API_PREFIX)
.path(action.getType().getPath())
.build();
log.info("action request url - " + build);
WebTarget target = client.target(build);
Response response = target.request().header(LRAClient.LRA_HTTP_HEADER, lraUri).post(Entity.json(lraInfo));
log.info("Result of action - " + response.readEntity(String.class));
Result result = response.getStatus() == Response.Status.OK.getStatusCode() ? Result.COMPLETED : Result.NEED_COMPENSATION;
response.close();
return result;
}
示例5: start
import javax.ws.rs.client.Client; //導入依賴的package包/類
public TestServiceRunner start() {
ServiceConfig serviceConfigwithProps = serviceConfig.addPropertiesAndApplyToBindings(propertyMap);
ServiceConfig serviceConfigWithContext = ServiceConfigInitializer.finalize(serviceConfigwithProps);
JerseyConfig jerseyConfig = new JerseyConfig(serviceConfigWithContext.serviceDefinition)
.addRegistrators(serviceConfigWithContext.registrators)
.addBinders(serviceConfigWithContext.binders);
serviceConfigWithContext.addons.forEach(it -> it.addToJerseyConfig(jerseyConfig));
DeploymentContext context = DeploymentContext.builder(jerseyConfig.getResourceConfig()).build();
URI uri = UriBuilder.fromUri("http://localhost/").port(0).build();
TestContainer testContainer = new InMemoryTestContainerFactory().create(uri, context);
testContainer.start();
ClientConfig clientConfig = testContainer.getClientConfig();
ClientGenerator clientGenerator = clientConfigurator.apply(
ClientGenerator.defaults(serviceConfigWithContext.serviceDefinition)
.clientConfigBase(clientConfig)
);
Client client = clientGenerator.generate();
Runtime runtime = new Runtime(serviceConfigWithContext, jerseyConfig, testContainer, clientConfig, uri, client, stubConfigurator, targetConfigurator);
return withServiceConfig(serviceConfigWithContext).withRuntime(runtime);
}
示例6: processRequest
import javax.ws.rs.client.Client; //導入依賴的package包/類
public static Response processRequest(
String url, String method, String payload, String authHeader) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Builder builder = target.request();
builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
if (authHeader != null) {
builder.header(HttpHeaders.AUTHORIZATION, authHeader);
}
return (payload != null)
? builder.build(method, Entity.json(payload)).invoke()
: builder.build(method).invoke();
}
示例7: removeServer
import javax.ws.rs.client.Client; //導入依賴的package包/類
public boolean removeServer(String url) {
for (int i = 0; i < serverTargetsList.size(); i++) {
try {
if (serverTargetsList.get(i).getUri().getHost().equals(new URI(url).getHost()) ) {
serverTargetsList.remove(i);
Client client = serverList.get(i);
if (client != null) {
client.close();
}
serverList.remove(i);
return true;
}
} catch (URISyntaxException ex) {
Logger.getLogger(ConnectorMThreatModuleRest.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
return false;
}
示例8: testCommit
import javax.ws.rs.client.Client; //導入依賴的package包/類
@Test
public void testCommit() throws SQLException, ClassNotFoundException, NamingException {
Client client = ClientBuilder.newClient();
client.target("http://127.0.0.1:8090/rest/test").request().get();
Class.forName("org.h2.Driver");
int resultCount = 0;
try (Connection conn = DriverManager.getConnection("jdbc:h2:~/data/testdb;AUTO_SERVER=TRUE", "SA", "SA")) {
ResultSet rs = conn.createStatement().executeQuery("SELECT ID, OWNER FROM TASK");
while (rs.next()) {
resultCount++;
}
}
Assert.assertEquals(1, resultCount);
}
示例9: testMPToken
import javax.ws.rs.client.Client; //導入依賴的package包/類
public void testMPToken() {
MPJWTToken mpjwtToken = tokenBuilder.setAudience("Octopus Rest MP").
setSubject("Octopus Test").build();
JWTParameters parameters = JWTParametersBuilder.newBuilderFor(JWTEncoding.JWS)
.withSecretKeyForSigning(jwkManager.getJWKSigningKey())
.build();
String bearerHeader = jwtEncoder.encode(mpjwtToken, parameters);
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest-mp/data/hello");
Response response = target.request(MediaType.APPLICATION_JSON)
.header(AUTHORIZATION_HEADER, BEARER + " " + bearerHeader)
.get();
System.out.println("status : " + response.getStatus());
System.out.println(response.readEntity(String.class));
}
示例10: getGreeting
import javax.ws.rs.client.Client; //導入依賴的package包/類
private Greeting getGreeting(String endpoint, String name) {
String endpointURL = endpoints.get(endpoint);
Client client = ClientBuilder.newClient();
try {
WebTarget target = client.target(endpointURL);
// Provide the authorization information
target.register((ClientRequestFilter) requestContext -> {
requestContext.getHeaders().add("Authorization", "Bearer "+token);
});
if(cmdArgs.debugLevel > 0)
target.register(new LoggingFilter());
IGreeting greetingClient = ((ResteasyWebTarget)target).proxy(IGreeting.class);
Greeting greeting = greetingClient.greeting(name);
return greeting;
} finally {
client.close();
}
}
示例11: shouldFailHealthcheckWhenMsaMetadataUnavailable
import javax.ws.rs.client.Client; //導入依賴的package包/類
@Test
public void shouldFailHealthcheckWhenMsaMetadataUnavailable() {
wireMockServer.stubFor(
get(urlEqualTo("/matching-service/metadata"))
.willReturn(aResponse()
.withStatus(500)
)
);
applicationTestSupport.before();
Client client = new JerseyClientBuilder(applicationTestSupport.getEnvironment()).build("test client");
Response response = client
.target(URI.create(String.format(HEALTHCHECK_URL, applicationTestSupport.getLocalPort())))
.request()
.buildGet()
.invoke();
String expectedResult = "\"msaMetadata\":{\"healthy\":false";
wireMockServer.verify(getRequestedFor(urlEqualTo("/matching-service/metadata")));
assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR.getStatusCode());
assertThat(response.readEntity(String.class)).contains(expectedResult);
}
示例12: trafficIsOk
import javax.ws.rs.client.Client; //導入依賴的package包/類
private boolean trafficIsOk(boolean expectedResult) throws Throwable {
if (isTrafficTestsEnabled()) {
System.out.println("=====> Send traffic");
long current = System.currentTimeMillis();
Client client = ClientBuilder.newClient(new ClientConfig());
Response result = client
.target(trafficEndpoint)
.path("/checkflowtraffic")
.queryParam("srcswitch", "s1")
.queryParam("dstswitch", "s8")
.queryParam("srcport", "1")
.queryParam("dstport", "1")
.queryParam("srcvlan", "1000")
.queryParam("dstvlan", "1000")
.request()
.get();
System.out.println(String.format("======> Response = %s", result.toString()));
System.out.println(String.format("======> Send traffic Time: %,.3f", getTimeDuration(current)));
return result.getStatus() == 200;
} else {
return expectedResult;
}
}
示例13: disconnectSwitch
import javax.ws.rs.client.Client; //導入依賴的package包/類
private boolean disconnectSwitch(String switchName) throws Exception {
System.out.println("\n==> Disconnect Switch");
long current = System.currentTimeMillis();
Client client = ClientBuilder.newClient(new ClientConfig());
Response result = client
.target(trafficEndpoint)
.path("/knockoutswitch")
.queryParam("switch", switchName)
.request()
.post(Entity.json(""));
System.out.println(String.format("===> Response = %s", result.toString()));
System.out.println(String.format("===> Disconnect Switch Time: %,.3f", getTimeDuration(current)));
return result.getStatus() == 200;
}
示例14: islDiscovered
import javax.ws.rs.client.Client; //導入依賴的package包/類
private boolean islDiscovered(String switchName, String portNo) throws Throwable {
System.out.println("\n==> Set ISL Discovered");
long current = System.currentTimeMillis();
Client client = ClientBuilder.newClient(new ClientConfig());
Response result = client
.target(trafficEndpoint)
.path("/restorelink")
.queryParam("switch", switchName)
.queryParam("port", portNo)
.request()
.post(Entity.json(""));
System.out.println(String.format("===> Response = %s", result.toString()));
System.out.println(String.format("===> Set ISL Discovered Time: %,.3f", getTimeDuration(current)));
return result.getStatus() == 200;
}
示例15: portUp
import javax.ws.rs.client.Client; //導入依賴的package包/類
private boolean portUp(String switchName, String portNo) throws Throwable {
System.out.println("\n==> Set Port Up");
long current = System.currentTimeMillis();
Client client = ClientBuilder.newClient(new ClientConfig());
Response result = client
.target(trafficEndpoint)
.path("/port/up")
.queryParam("switch", switchName)
.queryParam("port", portNo)
.request()
.post(Entity.json(""));
System.out.println(String.format("===> Response = %s", result.toString()));
System.out.println(String.format("===> Set Port Up Time: %,.3f", getTimeDuration(current)));
return result.getStatus() == 200;
}