java JAX-RS 2.0 - 如何从响应对象获取实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26864688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:46:55  来源:igfitidea点击:

JAX-RS 2.0 - How to get entity from Response object

javaweb-servicesjax-rsdropwizard

提问by Bartek Andrzejczak

I'm using JAX-RS 2.0 with Dropwizard 0.8.0-rc1 and I really can't figure out how to pull my entity from javax.ws.rs.core.Responseobject. response.getEntity()gives me ByteArrayOutputStream. I can create two requests - one that gives me headers and links and the other that gives me my response entity, but it seems like a stupid, wasteful and unclear thing to do. Is there a way for getting entity from response object?

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

My current test code is as follows:

我目前的测试代码如下:

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

}

回答by Paul Samsotha

You can use:

您可以使用: