【发布时间】:2015-06-24 18:17:25
【问题描述】:
我正在重写一些代码,因为我必须升级到最新版本的 Jersey (2.18),但我不明白为什么 fluent API 会以这种方式工作。
为什么会编译:
Response.Status.Family responseFamily = webTarget
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE))
.getStatusInfo()
.getFamily();
但这不是:
Response response = webTarget
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));
response.getStatusInfo().getFamily(); // the method getStatusInfo() isn't available here; why not?
作为参考,这是我的整个方法,它曾经在 Jersey 2.5.1 中运行良好,但由于 getStatusInfo() 方法,甚至无法在 2.18 中编译:
public void postGenericJson(Object entity, String... pathSegments) {
Response response;
WebTarget webTarget = httpsClient.target(apiBaseUrl);
try {
for (String pathSegment : pathSegments) {
webTarget = webTarget.path(pathSegment);
}
response = webTarget.request(MediaType.APPLICATION_JSON)
.post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));
log.trace("Issued POST to '" + webTarget.getUri().toString() + "'.");
} catch (Exception e) {
log.error("Error posting DTO: POST " + webTarget.getUri().toString(), e);
throw new RuntimeException("Error posting DTO.");
}
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
throw new ResponseProcessingException(response, "Error POSTing DTO");
}
}
【问题讨论】:
标签: java jersey-2.0 fluent-interface jersey-client