JAX-RS 2.0 - 如何从Response对象获取实体

Bar*_*zak 2 java web-services jax-rs dropwizard

我正在使用带有Dropwizard 0.8.0-rc1的JAX-RS 2.0,我真的无法弄清楚如何从javax.ws.rs.core.Response对象中拉出我的实体.response.getEntity()给了我ByteArrayOutputStream.我可以创建两个请求 - 一个给我标题和链接,另一个给我我的响应实体,但它似乎是一个愚蠢,浪费和不清楚的事情.有没有办法从响应对象中获取实体?

我目前的测试代码如下:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 6

您可以使用: