作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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);
}
}
最佳答案
您可以使用:
public abstract <T> T readEntity(Class<T> entityType)
- 使用支持将消息实体流映射到请求的类型的 MessageBodyReader 将消息实体输入流读取为指定 Java 类型的实例。 关于java - JAX-RS 2.0 - 如何从响应对象获取实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864688/
我是一名优秀的程序员,十分优秀!