gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:27:04 26 4
gpt4 key购买 nike

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

我当前的测试代码如下:

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

}

最佳答案

您可以使用:

关于java - JAX-RS 2.0 - 如何从响应对象获取实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com