我看到有两种方法可以使用JAX-RS中的客户端API构建目标URI
首先(借鉴here):
Client client;
WebTarget target;
@Inject HttpServletRequest httpServletRequest;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
target = client
.target("http://" +
httpServletRequest.getLocalName() +
":" +
httpServletRequest.getLocalPort() +
"/" +
httpServletRequest.getContextPath() +
"/webresources/movie/");
}
第二
Client client;
WebTarget target;
@Context UriInfo uriInfo;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
String uri = uriInfo.getAbsolutePath().toString();
target = client
.target(uri + "/webresources/movie/");
}
第一个使用HttpServletRequest
,第二个使用UriInfo
进行注射。在任何情况下都使用一个优于另一个。如果是的话?