javax.ws.rs.core.Response.getEntity()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(180)

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

Response.getEntity介绍

[英]Get the message entity Java instance. Returns null if the message does not contain an entity body.

If the entity is represented by an un-consumed InputStreamthe method will return the input stream.
[中]获取消息实体Java实例。如果消息不包含实体正文,则返回null。
如果实体由未使用的InputStream表示,则该方法将返回输入流。

代码示例

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

private static Builder handleHeartbeat(EurekaServerConfig config, InstanceResource resource, String lastDirtyTimestamp, String overriddenStatus, String instanceStatus) {
  Response response = resource.renewLease(REPLICATION, overriddenStatus, instanceStatus, lastDirtyTimestamp);
  int responseStatus = response.getStatus();
  Builder responseBuilder = new Builder().setStatusCode(responseStatus);
  if ("false".equals(config.getExperimental("bugfix.934"))) {
    if (responseStatus == Status.OK.getStatusCode() && response.getEntity() != null) {
      responseBuilder.setResponseEntity((InstanceInfo) response.getEntity());
    }
  } else {
    if ((responseStatus == Status.OK.getStatusCode() || responseStatus == Status.CONFLICT.getStatusCode())
        && response.getEntity() != null) {
      responseBuilder.setResponseEntity((InstanceInfo) response.getEntity());
    }
  }
  return responseBuilder;
}

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

Form form = new Form();
form.add("x", "foo");
form.add("y", "bar");

Client client = ClientBuilder.newClient();

WebTarget resource = client.target("http://localhost:8080/someresource");

Builder request = resource.request();
request.accept(MediaType.APPLICATION_JSON);

Response response = request.get();

if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
  System.out.println("Success! " + response.getStatus());
  System.out.println(response.getEntity());
} else {
  System.out.println("ERROR! " + response.getStatus());    
  System.out.println(response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testErrorDELETE()
{
 final Response response = abstractListenerHandler.handleDELETE(error_id);
 Assert.assertEquals(500, response.getStatus());
 Assert.assertEquals(ImmutableMap.of("error", error_msg), response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testGetTasksNegativeState()
{
 EasyMock.replay(taskRunner, taskMaster, taskStorageQueryAdapter, indexerMetadataStorageAdapter, req);
 Object responseObject = overlordResource
   .getTasks("blah", "ds_test", null, null, null, req)
   .getEntity();
 Assert.assertEquals(
   "Invalid state : blah, valid values are: [pending, waiting, running, complete]",
   responseObject.toString()
 );
}

代码示例来源:origin: confluentinc/ksql

@Test
public void shoudlFailIfCannotWriteTerminateCommand() throws InterruptedException {
 // Given:
 when(commandStore.enqueueCommand(any(), any(), any(), any())).thenThrow(new KsqlException(""));
 // When:
 final Response response = ksqlResource.terminateCluster(VALID_TERMINATE_REQUEST);
 // Then:
 assertThat(response.getStatus(), equalTo(500));
 assertThat(response.getEntity().toString(),
   CoreMatchers
     .startsWith("Could not write the statement 'TERMINATE CLUSTER;' into the command "));
}

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

@Test
public void shouldAdvertiseUriForQueringAllRelationsInTheDatabase()
{
  Response response = service.getRoot();
  assertThat( new String( (byte[]) response.getEntity() ),
      containsString( "\"relationship_types\" : \"http://neo4j.org/relationship/types\"" ) );
}

代码示例来源:origin: apache/incubator-druid

@SuppressWarnings({"ConstantConditions"})
private int getNumSubTasks(Function<SinglePhaseParallelIndexingProgress, Integer> func)
{
 final Response response = task.getProgress(newRequest());
 Assert.assertEquals(200, response.getStatus());
 return func.apply((SinglePhaseParallelIndexingProgress) response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHandleAll()
{
 final Response response = abstractListenerHandler.handleGETAll();
 Assert.assertEquals(200, response.getStatus());
 Assert.assertEquals(all, response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testGetClusterServersSimple() throws Exception
{
 Response res = serversResource.getClusterServers(null, "simple");
 String result = objectMapper.writeValueAsString(res.getEntity());
 String expected = "[{\"host\":\"host\",\"tier\":\"tier\",\"type\":\"historical\",\"priority\":0,\"currSize\":1,\"maxSize\":1234}]";
 Assert.assertEquals(expected, result);
}

代码示例来源:origin: confluentinc/ksql

@Test
 public void shouldReturnCorrectResponseForUnspecificException() {
  final Response response = exceptionMapper.toResponse(new Exception("error msg"));
  assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class));
  final KsqlErrorMessage errorMessage = (KsqlErrorMessage)response.getEntity();
  assertThat(errorMessage.getMessage(), equalTo("error msg"));
  assertThat(errorMessage.getErrorCode(), equalTo(Errors.ERROR_CODE_SERVER_ERROR));
  assertThat(response.getStatus(), equalTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
 }
}

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

@Test
public void shouldReturnValidJSON() throws Exception
{
  Response response = testDiscoveryService().getDiscoveryDocument( uriInfo( baseUri ) );
  String json = new String( (byte[]) response.getEntity() );
  assertNotNull( json );
  assertThat( json.length(), is( greaterThan( 0 ) ) );
  assertThat( json, is( not( "\"\"" ) ) );
  assertThat( json, is( not( "null" ) ) );
}

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

private <T> void assertEqualsJsonResponse(Response expected, Response actual, Class<T> entityClass) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    T entityFromExpected = objectMapper.readValue((String) expected.getEntity(), entityClass);
    T actualFromExpected = objectMapper.readValue((String) expected.getEntity(), entityClass);
    assertEquals(entityFromExpected, actualFromExpected);

    assertEquals(expected.getStatus(), actual.getStatus());
    assertTrue(expected.getHeaders().equalsIgnoreValueOrder(actual.getHeaders()));
  }
}

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

Form form = new Form();
form.add("x", "foo");
form.add("y", "bar");

ClientResource resource = new ClientResource("http://localhost:8080/someresource");

Response response = resource.post(form.getWebRepresentation());

if (response.getStatus().isSuccess()) {
  System.out.println("Success! " + response.getStatus());
  System.out.println(response.getEntity().getText());
} else {
  System.out.println("ERROR! " + response.getStatus());
  System.out.println(response.getEntity().getText());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testExceptionalHandleAll()
{
 shouldFail.set(true);
 final Response response = abstractListenerHandler.handleGETAll();
 Assert.assertEquals(500, response.getStatus());
 Assert.assertEquals(ImmutableMap.of("error", error_message), response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testGetClusterServersFull() throws Exception
{
 Response res = serversResource.getClusterServers("full", null);
 String result = objectMapper.writeValueAsString(res.getEntity());
 String expected = "[{\"host\":\"host\","
          + "\"maxSize\":1234,"
          + "\"type\":\"historical\","
          + "\"tier\":\"tier\","
          + "\"priority\":0,"
          + "\"segments\":{\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\":"
          + "{\"dataSource\":\"dataSource\",\"interval\":\"2016-03-22T14:00:00.000Z/2016-03-22T15:00:00.000Z\",\"version\":\"v0\",\"loadSpec\":{},\"dimensions\":\"\",\"metrics\":\"\","
          + "\"shardSpec\":{\"type\":\"none\"},\"binaryVersion\":null,\"size\":1,\"identifier\":\"dataSource_2016-03-22T14:00:00.000Z_2016-03-22T15:00:00.000Z_v0\"}},"
          + "\"currSize\":1}]";
 Assert.assertEquals(expected, result);
}

代码示例来源:origin: confluentinc/ksql

@Test
public void shouldReturnCorrectResponseForWebAppException() {
 final WebApplicationException webApplicationException = new WebApplicationException("error msg", 403);
 final Response response = exceptionMapper.toResponse(webApplicationException);
 assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class));
 final KsqlErrorMessage errorMessage = (KsqlErrorMessage)response.getEntity();
 assertThat(errorMessage.getMessage(), equalTo("error msg"));
 assertThat(errorMessage.getErrorCode(), equalTo(40300));
 assertThat(response.getStatus(), equalTo(403));
}

代码示例来源:origin: Alluxio/alluxio

@Test
public void getConfiguration() {
 Response response = mHandler.getConfiguration();
 try {
  assertNotNull("Response must be not null!", response);
  assertNotNull("Response must have a entry!", response.getEntity());
  assertTrue("Entry must be a SortedMap!", (response.getEntity() instanceof SortedMap));
  SortedMap<String, String> entry = (SortedMap<String, String>) response.getEntity();
  assertFalse("Properties Map must be not empty!", (entry.isEmpty()));
 } finally {
  response.close();
 }
}

代码示例来源:origin: confluentinc/ksql

private KsqlErrorMessage makeFailingRequest(final KsqlRequest ksqlRequest, final Code errorCode) {
 try {
  final Response response = ksqlResource.handleKsqlStatements(ksqlRequest);
  assertThat(response.getStatus(), is(errorCode.getCode()));
  assertThat(response.getEntity(), instanceOf(KsqlErrorMessage.class));
  return (KsqlErrorMessage) response.getEntity();
 } catch (final KsqlRestException e) {
  return (KsqlErrorMessage) e.getResponse().getEntity();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHandleSimpleDELETE()
{
 final Response response = abstractListenerHandler.handleDELETE(valid_id);
 Assert.assertEquals(202, response.getStatus());
 Assert.assertEquals(obj, response.getEntity());
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testGetServerSimple() throws Exception
{
 Response res = serversResource.getServer(server.getName(), "simple");
 String result = objectMapper.writeValueAsString(res.getEntity());
 String expected = "{\"host\":\"host\",\"tier\":\"tier\",\"type\":\"historical\",\"priority\":0,\"currSize\":1,\"maxSize\":1234}";
 Assert.assertEquals(expected, result);
}

相关文章