Java JAX-RS2.0-如何从响应对象获取实体

Java JAX-RS2.0-如何从响应对象获取实体,java,web-services,jax-rs,dropwizard,Java,Web Services,Jax Rs,Dropwizard,我将JAX-RS2.0与Dropwizard 0.8.0-rc1一起使用,我真的不知道如何从javax.ws.RS.core.Response对象中提取实体response.getEntity()通过tearrayoutputstream提供。我可以创建两个请求——一个给我标题和链接,另一个给我响应实体,但这似乎是一件愚蠢、浪费和不清楚的事情。有没有从响应对象获取实体的方法 我目前的测试代码如下: public class GroupsResourceTest { public sta

我将JAX-RS2.0与Dropwizard 0.8.0-rc1一起使用,我真的不知道如何从
javax.ws.RS.core.Response
对象中提取实体
response.getEntity()
通过tearrayoutputstream提供
。我可以创建两个请求——一个给我标题和链接,另一个给我响应实体,但这似乎是一件愚蠢、浪费和不清楚的事情。有没有从响应对象获取实体的方法

我目前的测试代码如下:

public class GroupsResourceTest {

    public static String CONFIGURATION_FILE = "src/test/resources/test-conf.yml";

    @ClassRule
    public final static DropwizardAppRule<BpmConsoleConfiguration> RULE =
            new DropwizardAppRule<>(BpmConsoleApplication.class, CONFIGURATION_FILE);

    static Client client;

    @BeforeClass
    public static void initClient(){
        client = new JerseyClientBuilder(RULE.getEnvironment()).build("client");
        client.register(HttpAuthenticationFeature.basic(User.ADMIN.login, User.ADMIN.password));
    }

    @Test
    public void shouldGetGroups() {
        //when
        WebTarget resource = target("/groups");
        List<String> groups = resource.request().get(new GenericType<>(List.class)); //first request
        Response response = resource.request().get(); //second request
        Link self = response.getLink("self");
        //then
        assertThat(self.getUri().getPath()).isEqualTo("/groups");
        assertThat(groups).contains(User.ADMIN.login);

    }

    public WebTarget target(String path){
        String url = String.format("http://localhost:%d%s", RULE.getLocalPort(), path);
        return client.target(url);
    }

}
公共类组资源测试{
公共静态字符串配置_FILE=“src/test/resources/test-conf.yml”;
@阶级规则
公共最终静态DropwizardAppRule规则=
新DropwizardAppRule(BpmConsoleApplication.class,配置文件);
静态客户端;
@课前
公共静态void initClient(){
client=newjerseyclientbuilder(RULE.getEnvironment()).build(“客户机”);
注册(HttpAuthenticationFeature.basic(User.ADMIN.login,User.ADMIN.password));
}
@试验
public void shouldGetGroups(){
//什么时候
WebTarget资源=目标(“/groups”);
列表组=resource.request().get(新的GenericType(List.class));//第一个请求
Response Response=resource.request().get();//第二个请求
Link self=response.getLink(“self”);
//然后
assertThat(self.getUri().getPath()).isEqualTo(“/groups”);
assertThat(groups.contains)(User.ADMIN.login);
}
公共WebTarget目标(字符串路径){
字符串url=String.format(“http://localhost:%d%s,RULE.getLocalPort(),path);
返回client.target(url);
}
}
您可以使用:

  • -使用支持将消息实体流映射到请求类型的MessageBodyReader,将消息实体输入流作为指定Java类型的实例读取
我的意思是什么是用例。什么时候应该在出站响应上设置getEntity和readEntity?@CroskCool getEntity,在入站响应上设置readEntity