我需要在Jersey上做一个代理API服务.我需要在泽西方法中拥有完整的请求URL.我不想指定所有可能的参数.
例如:
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/media.json")
public String getMedia( ){
// here I want to get the full request URL like /media.json?param1=value1¶m2=value2
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
Ale*_*yon 14
在Jersey 2.x中(注意它使用HttpServletRequest对象):
@GET
@Path("/test")
public Response test(@Context HttpServletRequest request) {
String url = request.getRequestURL().toString();
String query = request.getQueryString();
String reqString = url + "?" + query;
return Response.status(Status.OK).entity(reqString).build();
}
Run Code Online (Sandbox Code Playgroud)
尝试 UriInfo 如下,
@POST
@Consumes({ MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON})
@Path("add")
public Response addSuggestionAndFeedback(@Context UriInfo uriInfo, Student student) {
System.out.println(uriInfo.getAbsolutePath());
.........
}
Run Code Online (Sandbox Code Playgroud)
输出:- https://localhost:9091/api/suggestionandfeedback/add
您也可以尝试以下选项