com.sun.jersey.api.client.Client.resource()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(272)

本文整理了Java中com.sun.jersey.api.client.Client.resource()方法的一些代码示例,展示了Client.resource()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Client.resource()方法的具体详情如下:
包路径:com.sun.jersey.api.client.Client
类名称:Client
方法名:resource

Client.resource介绍

[英]Create a Web resource from the client.
[中]从客户端创建Web资源。

代码示例

代码示例来源:origin: stackoverflow.com

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/path");
ClientResponse response = webResource.accept("application/json")
                   .get(ClientResponse.class);

代码示例来源:origin: stackoverflow.com

Client client = Client.create();
WebResource webResource = client.resource(baseuri);
ClientResponse response = webResource.accept("application/json")
    .type("application/json").post(ClientResponse.class, input);
if (response.getStatus() != 200) {
  throw new RuntimeException("Failed : HTTP error code : "
      + response.getStatus());
String output = response.getEntity(String.class);
System.out.println("Output from Server .... ");
System.out.println(output + "\n");

代码示例来源:origin: stackoverflow.com

Client client = Client.create();
WebResource webResource =client.resource("uri");

MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
queryParams.add("json", js); //set parametes for request

appKey = "Bearer " + appKey; // appKey is unique number

//Get response from RESTful Server get(ClientResponse.class);
ClientResponse response = null;
response = webResource.queryParams(queryParams)
            .header("Content-Type", "application/json;charset=UTF-8")
  .header("Authorization", appKey)
  .get(ClientResponse.class);

String jsonStr = response.getEntity(String.class);

代码示例来源:origin: pentaho/pentaho-kettle

public <T> T getAsJson( Class<T> returnType ) {
 UriBuilder localUriBuilder = _uriBuilder.clone();
 com.sun.jersey.api.client.WebResource resource =
   _client.resource( localUriBuilder.buildFromMap( _templateAndMatrixParameterValues ) );
 com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
 resourceBuilder = resourceBuilder.accept( "application/json" );
 com.sun.jersey.api.client.ClientResponse response;
 response = resourceBuilder.method( "GET", com.sun.jersey.api.client.ClientResponse.class );
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  if ( response.getStatus() >= 400 ) {
   throw new PentahoDiPlugin.WebApplicationExceptionMessage( Response.status(
     response.getClientResponseStatus() ).build() );
  }
 }
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  return response.getEntity( returnType );
 } else {
  return returnType.cast( response );
 }
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldPickFirstXForwardedHostHeaderValueFromCommaOrCommaAndSpaceSeparatedList()
{
  // when
  ClientResponse response = client.resource( getManageUri() )
      .accept( APPLICATION_JSON )
      .header( X_FORWARDED_HOST, "jimwebber.org, kathwebber.com,neo4j.org" )
      .get( ClientResponse.class );
  // then
  String entity = response.getEntity( String.class );
  assertTrue( entity.contains( "http://jimwebber.org" ) );
  assertFalse( entity.contains( "http://localhost" ) );
}

代码示例来源:origin: Netflix/eureka

@Override
public EurekaHttpResponse<Void> cancel(String appName, String id) {
  String urlPath = "apps/" + appName + '/' + id;
  ClientResponse response = null;
  try {
    Builder resourceBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder();
    addExtraHeaders(resourceBuilder);
    response = resourceBuilder.delete(ClientResponse.class);
    return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
  } finally {
    if (logger.isDebugEnabled()) {
      logger.debug("Jersey HTTP DELETE {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
    }
    if (response != null) {
      response.close();
    }
  }
}

代码示例来源:origin: Netflix/conductor

Builder builder = client.resource(input.uri).type(input.contentType);
  if (cr.getStatus() != 204 && cr.hasEntity()) {
    response.body = extractBody(cr);
  response.statusCode = cr.getStatus();
  response.reasonPhrase = cr.getStatusInfo().getReasonPhrase();
  response.headers = cr.getHeaders();
  logger.error(String.format("Got unexpected http response - uri: %s, vipAddress: %s, status code: %s", input.getUri(), input.getVipAddress(), cr.getStatus()), ex);
  if(cr.getStatus() > 199 && cr.getStatus() < 300) {
    if(cr.getStatus() != 204 && cr.hasEntity()) {
    return response;
  }else {
    String reason = cr.getEntity(String.class);
    logger.error(reason, ex);
    throw new Exception(reason);

代码示例来源:origin: Netflix/conductor

private <T> T getForEntity(String url, Object[] queryParams, Function<ClientResponse, T> entityProvider, Object... uriVariables) {
  URI uri = null;
  ClientResponse clientResponse;
  try {
    uri = getURIBuilder(root + url, queryParams).build(uriVariables);
    clientResponse = client.resource(uri)
        .accept(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN)
        .get(ClientResponse.class);
    if (clientResponse.getStatus() < 300) {
      return entityProvider.apply(clientResponse);
    } else {
      throw new UniformInterfaceException(clientResponse);
    }
  } catch (UniformInterfaceException e) {
    handleUniformInterfaceException(e, uri);
  } catch (RuntimeException e) {
    handleRuntimeException(e, uri);
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

ClientConfig cc = new DefaultClientConfig();
 cc.getProperties().put(
     ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
 Client c = Client.create(cc);
 WebResource r = c.resource("https://my_url");
 ClientResponse response = r.get(ClientResponse.class);
 EntityTag e = response.getEntityTag();
 String entity = response.getEntity(String.class);
 System.out.println("The response status is " + response.getStatus());
 System.out.println("The enttiy body is " + entity);

代码示例来源:origin: pentaho/pentaho-kettle

public <T> T getAsJson( Class<T> returnType ) {
 UriBuilder localUriBuilder = _uriBuilder.clone();
 com.sun.jersey.api.client.WebResource resource =
   _client.resource( localUriBuilder.buildFromMap( _templateAndMatrixParameterValues ) );
 com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
 resourceBuilder = resourceBuilder.accept( "application/json" );
 com.sun.jersey.api.client.ClientResponse response;
 response = resourceBuilder.method( "GET", com.sun.jersey.api.client.ClientResponse.class );
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  if ( response.getStatus() >= 400 ) {
   throw new PentahoDiPlugin.WebApplicationExceptionMessage( Response.status(
     response.getClientResponseStatus() ).build() );
  }
 }
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  return response.getEntity( returnType );
 } else {
  return returnType.cast( response );
 }
}

代码示例来源:origin: stackoverflow.com

HttpClient apacheClient = HttpClientBuilder.create().build();
Client client = new Client(new ApacheHttpClient4Handler(apacheClient,
                            new BasicCookieStore(),
                            true));
WebResource webResource = client.resource("http://localhost:8080/path");
ClientResponse response = webResource.accept("application/json")
                   .get(ClientResponse.class);

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldUseXForwardedProtoHeaderWhenPresent()
{
  // when
  ClientResponse response = client.resource( getManageUri() )
      .accept( APPLICATION_JSON )
      .header( X_FORWARDED_PROTO, "https" )
      .get( ClientResponse.class );
  // then
  String entity = response.getEntity( String.class );
  assertTrue( entity.contains( "https://localhost" ) );
  assertFalse( entity.contains( "http://localhost" ) );
}

代码示例来源:origin: Netflix/Priam

Client client = Client.create(config);
String baseURI = getBaseURI(host);
WebResource service = client.resource(baseURI);
  clientResp =
      service.path("Priam/REST/v1/cassadmin/gossipinfo")
          .accept(MediaType.APPLICATION_JSON)
          .get(ClientResponse.class);
  if (clientResp.getStatus() != 200) return null;
  textEntity = clientResp.getEntity(String.class);

代码示例来源:origin: Netflix/eureka

@Override
public EurekaHttpResponse<Void> deleteStatusOverride(String appName, String id, InstanceInfo info) {
  String urlPath = "apps/" + appName + '/' + id + "/status";
  ClientResponse response = null;
  try {
    Builder requestBuilder = jerseyClient.resource(serviceUrl)
        .path(urlPath)
        .queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString())
        .getRequestBuilder();
    addExtraHeaders(requestBuilder);
    response = requestBuilder.delete(ClientResponse.class);
    return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
  } finally {
    if (logger.isDebugEnabled()) {
      logger.debug("Jersey HTTP DELETE {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
    }
    if (response != null) {
      response.close();
    }
  }
}

代码示例来源:origin: stackoverflow.com

public class Test {  
  public static void main(String[] args) {
     ClientConfig config = new DefaultClientConfig();
     Client client = Client.create(config);
     WebResource service = client.resource(getBaseURI());
     // Now do the MAP stuff
     MyHashMapObject<String, String> map = new MyHashMapObject<String, String>();
     map.getMapProperty().put("a", "1");
     map.getMapProperty().put("b", "2");
     ClientResponse response = service.path("rest").path("hello2")
       .type(MediaType.APPLICATION_XML)
       .accept(MediaType.APPLICATION_XML)
       .post(ClientResponse.class, map);
     Map<String, String> myMap = response.getEntity(MyHashMapObject.class).getMapProperty();     
     for(Map.Entry<String, String> entry : myMap.entrySet())
       System.out.format("Key: %s, Value: %s\n", entry.getKey(), entry.getValue());
   }
   private static URI getBaseURI() {
     return UriBuilder.fromUri(
         "http://localhost:8080/org.nowaq.jersey.first").build();
   }
 }

代码示例来源:origin: pentaho/pentaho-kettle

public <T> T getAsXml( Class<T> returnType ) {
 UriBuilder localUriBuilder = _uriBuilder.clone();
 com.sun.jersey.api.client.WebResource resource =
   _client.resource( localUriBuilder.buildFromMap( _templateAndMatrixParameterValues ) );
 com.sun.jersey.api.client.WebResource.Builder resourceBuilder = resource.getRequestBuilder();
 resourceBuilder = resourceBuilder.accept( "application/xml" );
 com.sun.jersey.api.client.ClientResponse response;
 response = resourceBuilder.method( "GET", com.sun.jersey.api.client.ClientResponse.class );
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  if ( response.getStatus() >= 400 ) {
   throw new PentahoDiPlugin.WebApplicationExceptionMessage( Response.status(
     response.getClientResponseStatus() ).build() );
  }
 }
 if ( !com.sun.jersey.api.client.ClientResponse.class.isAssignableFrom( returnType ) ) {
  return response.getEntity( returnType );
 } else {
  return returnType.cast( response );
 }
}

代码示例来源:origin: stackoverflow.com

Client client = Client.create();
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource("http://localhost:9998/");
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                     .get(ClientResponse.class);

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldUseXForwardedHostHeaderWhenPresent()
{
  // when
  ClientResponse response = client.resource( getManageUri() )
      .accept( APPLICATION_JSON )
      .header( X_FORWARDED_HOST, "jimwebber.org" )
      .get( ClientResponse.class );
  // then
  String entity = response.getEntity( String.class );
  assertTrue( entity.contains( "http://jimwebber.org" ) );
  assertFalse( entity.contains( "http://localhost" ) );
}

代码示例来源:origin: apache/cloudstack

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource(UriBuilder.fromUri(restprotocol + elastiCenterAddress + restpath).build());
  System.out.println("Command Sent " + command + " : " + queryParams);
ClientResponse response = webResource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
if (response.getStatus() >= 300) {
  if (debug)
    System.out.println("ElastiCenter returned error code : " + response.getStatus());
  if (401 == response.getStatus()) {
    throw new InvalidCredentialsException("Please specify a valid API Key.");
  } else if (431 == response.getStatus()) {
  String jsonResponse = response.getEntity(String.class);
  if (debug) {
    System.out.println("Command Response : " + jsonResponse);

代码示例来源:origin: Netflix/eureka

@Override
public EurekaHttpResponse<Void> statusUpdate(String appName, String id, InstanceStatus newStatus, InstanceInfo info) {
  String urlPath = "apps/" + appName + '/' + id + "/status";
  ClientResponse response = null;
  try {
    Builder requestBuilder = jerseyClient.resource(serviceUrl)
        .path(urlPath)
        .queryParam("value", newStatus.name())
        .queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString())
        .getRequestBuilder();
    addExtraHeaders(requestBuilder);
    response = requestBuilder.put(ClientResponse.class);
    return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
  } finally {
    if (logger.isDebugEnabled()) {
      logger.debug("Jersey HTTP PUT {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus());
    }
    if (response != null) {
      response.close();
    }
  }
}

相关文章