本文整理汇总了Java中javax.ws.rs.client.WebTarget.request方法的典型用法代码示例。如果您正苦于以下问题:Java WebTarget.request方法的具体用法?Java WebTarget.request怎么用?Java WebTarget.request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.ws.rs.client.WebTarget
的用法示例。
在下文中一共展示了WebTarget.request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makePost
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
/**
* Wrap the supplied element in a SOAP wrapper and post to the specified end-point
*
* @return Response from the remote server
* @throws SOAPRequestError if the remote server returns a non-200 status code
* @throws ResponseProcessingException in case processing of a received HTTP response fails (e.g. in a filter
* or during conversion of the response entity data to an instance
* of a particular Java type).
* @throws ProcessingException in case the request processing or subsequent I/O operation fails.
*/
protected SoapResponse makePost(URI uri, Element requestElement) throws SOAPRequestError {
LOG.info(format("Making SOAP request to: {0}", uri));
Document requestDocument = soapMessageManager.wrapWithSoapEnvelope(requestElement);
WebTarget target = client.target(uri);
final Invocation.Builder request = target.request();
final Response response = request.post(Entity.entity(requestDocument, MediaType.TEXT_XML_TYPE));
try {
if (response.getStatus() != 200) {
LOG.warn(format("Unexpected status code ({0}) when contacting ({1}))", response.getStatus(), uri));
// let the calling code handle this issue appropriately
throw new SOAPRequestError(response);
} else {
try {
return giveMeMySoap(response);
} catch(BadRequestException e) {
LOG.warn(format("Couldn't parse SOAP response when contacting ({0}))", uri), e);
throw new SOAPRequestError(response, e);
}
}
} finally {
// Ensure that response's input stream has been closed. This may not happen automatically in some cases
// (e.g. the response body is never read from).
try {
response.close();
} catch (ProcessingException f) {
LOG.warn("Problem closing Jersey connection.", f);
}
}
}
示例2: processRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的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();
}
示例3: makeConnection
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
/** Make an HTTP connection to the specified URL and pass in the specified payload. */
private static Response makeConnection(
String method, String urlString, String payload, String jwt)
throws IOException, GeneralSecurityException {
// Setup connection
System.out.println("Creating connection - Method: " + method + ", URL: " + urlString);
Client client = ClientBuilder.newClient();
WebTarget target = client.target(urlString);
Invocation.Builder invoBuild = target.request(MediaType.APPLICATION_JSON_TYPE);
if (jwt != null) {
invoBuild.header("Authorization", jwt);
}
if (payload != null) {
System.out.println("Request Payload: " + payload);
Entity<String> data = Entity.entity(payload, MediaType.APPLICATION_JSON_TYPE);
return invoBuild.build(method, data).invoke();
} else {
return invoBuild.build(method).invoke();
}
}
示例4: sendRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
/**
* Helper method to send a simple http request without a payload
*
* @param url the url to call
* @param requestType the http method to invoke
* @return the response from the server
*/
public Response sendRequest(String url, String requestType) {
String method = "sendRequest";
logger.entering(clazz, method);
logger.fine(" url: " + url);
logger.fine(" requestType: " + requestType);
String jwt = null;
try {
jwt = new JWTVerifier().createJWT("fred");
} catch (Throwable t) {
throw new RuntimeException(t);
}
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Invocation.Builder invoBuild = target.request();
invoBuild.header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt);
Response response = invoBuild.build(requestType).invoke();
logger.exiting(
clazz, method, "\n\n- - - - - Exiting " + method + " with: " + response + "\n\n");
return response;
}
示例5: main
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
public static void main(String[] args)
throws IOException, URISyntaxException {
Client client = ClientBuilder.newClient();
// GET http://api.geonames.org/searchJSON?...
WebTarget target = client.target("http://api.geonames.org/searchJSON")
.queryParam("q", "kabupaten garut")
.queryParam("username", "ceefour");
Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
GeonamesSearchResult searchResult = invocationBuilder.get(GeonamesSearchResult.class);
LOG.info("Body (GeonamesSearchResult): {}", searchResult);
for (Geoname child : searchResult.getGeonames()) {
LOG.info("Place: {} ({}, {})",
child.getToponymName(), child.getLat(), child.getLng());
}
}
示例6: getRequestForTestCase
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
private Invocation.Builder getRequestForTestCase(RedirectorTestCase redirectorTestCase, String appName) {
WebTarget webTarget = new ServiceHelper(endpointUrlForApp(appName)).getWebTarget();
webTarget = webTarget.queryParam(DataParamName.testInfo.name(), redirectorTestCase.getName());
webTarget = webTarget.queryParam(Context.SESSION_ID, redirectorTestCase.getName());
for (Parameter parameter : redirectorTestCase.getParameters()) {
List<Value> values = parameter.getValues();
Value value = CollectionUtils.isNotEmpty(values) ? values.get(0) : new Value("");
webTarget = webTarget.queryParam(parameter.getName(), value);
}
return webTarget.request();
}
示例7: restGet
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
/**
* Returns response of the REST get operation with a given additional path.
*
* @param path path or null
* @return response json string
*/
public String restGet(String path) {
WebTarget wt = client.target(access.endpoint() + baseUrl).path(path);
Invocation.Builder builder = wt.request(JSON_UTF_8.toString());
try {
Response response = builder.get();
if (response.getStatus() != HTTP_OK) {
log.warn("Failed to get resource {}", access.endpoint() + baseUrl + path);
return EMPTY_JSON_STRING;
}
} catch (javax.ws.rs.ProcessingException e) {
return EMPTY_JSON_STRING;
}
return builder.get(String.class);
}
示例8: request
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
private Invocation.Builder request(String path, Map<String, String> queryParams) {
WebTarget target = httpClient.target(getBaseUri() + BASE_PATH).path(path);
if (queryParams != null && !queryParams.isEmpty()) {
Set<String> keys = queryParams.keySet();
for (String key : keys) {
target = target.queryParam(key, queryParams.get(key));
}
}
return target.request();
}
示例9: sendRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
public Response sendRequest(String url, String requestType) {
Client client = ClientBuilder.newClient();
System.out.println("Testing " + url);
WebTarget target = client.target(url);
Invocation.Builder invoBuild = target.request();
Response response = invoBuild.build(requestType).invoke();
return response;
}
示例10: processRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
public 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();
}
示例11: processRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
private 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();
}
示例12: processRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的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();
if (payload != null) {
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();
}
示例13: sendRequestJson
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
/**
* Helper method for sending requests with a json payload.
*
* @param url the url to call
* @param requestType the http method to invoke
* @param json the json payload to send
* @return the response from the server
*/
public Response sendRequestJson(String url, String requestType, String json) {
String method = "sendRequestJson";
logger.entering(clazz, method);
logger.fine(" url: " + url);
logger.fine(" requestType: " + requestType);
logger.fine(" json: " + json);
String jwt = null;
try {
jwt = new JWTVerifier().createJWT("fred");
} catch (Throwable t) {
throw new RuntimeException(t);
}
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Invocation.Builder invoBuild = target.request(MediaType.APPLICATION_JSON_TYPE);
// Allowing for null payload to get a 400 response code
Entity<String> data;
if (null == json || json.isEmpty()) {
data = Entity.entity("", MediaType.APPLICATION_JSON_TYPE);
} else {
data = Entity.entity(json, MediaType.APPLICATION_JSON_TYPE);
}
invoBuild.header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt);
Response response = invoBuild.build(requestType, data).invoke();
logger.exiting(
clazz,
method,
"\n\n- - - - - Exiting " + method + " with: " + response.readEntity(String.class) + "\n\n");
return response;
}
示例14: processRequest
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
public Response processRequest(String url, String method, String payload)
throws GeneralSecurityException, IOException {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Builder builder = target.request();
builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
builder.header(
HttpHeaders.AUTHORIZATION,
"Bearer "
+ new JWTVerifier()
.createJWT("fred", new HashSet<String>(Arrays.asList("orchestrator"))));
return (payload != null)
? builder.build(method, Entity.json(payload)).invoke()
: builder.build(method).invoke();
}
示例15: playRound
import javax.ws.rs.client.WebTarget; //导入方法依赖的package包/类
@Override
protected RoundResult playRound(Round round) {
logger.info("Playing round: " + round.getName());
Iterator<GameTicket> itr = getStatus().getTickets().iterator();
while (itr.hasNext()) {
GameTicket player = itr.next();
logger.debug("Requesting cards from " + player.getPlayerName());
// Ask player for cards
Client client = ClientBuilder.newClient(new ClientConfig().register( LoggingFeature.class ));
WebTarget webTarget = client.target(player.getPlayerEndpoint()).path("player/round");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.put(Entity.entity(round, MediaType.APPLICATION_JSON));
if (response.getStatus() != 200) {
logger.warn("Response from " + player.getPlayerName() + " indicates a lack of success.");
String msg = response.readEntity(String.class);
logger.warn(response.getStatus() + " - " + msg);
}
PlayedCards cards = response.readEntity(PlayedCards.class);
round.addCards(player, cards);
}
RoundResult results = round.getResults();
logger.info("Round is complete. Results:\n" + results.toString());
return results;
}