我需要做的与新泽西州代理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
}
我该怎么做?
在新泽西州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();
}
如果你需要一个智能代理,你可以得到的参数,筛选它们,并创建一个新的URL。
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/media.json")
public String getMedia(@Context HttpServletRequest hsr){
Enumeration parameters = hsr.getParameterNames();
while (parameters.hasMoreElements()) {
String key = (String) parameters.nextElement();
String value = hsr.getParameter(key);
//Here you can add values to a new string: key + "=" + value + "&";
}
}
尝试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());
.........
}
OUTPUT: - qazxsw POI
你可以尝试以下选项也
您可以使用过滤器新泽西州。
之后,美必须以HTTP配置文件中注册它,还是延长ResourceConfig类。这是如何U可以在HTTP配置类其注册
public class HTTPFilter implements ContainerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(HTTPFilter.class);
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
logger.info(containerRequestContext.getUriInfo().getPath() + " endpoint called...");
//logger.info(containerRequestContext.getUriInfo().getAbsolutePath() + " endpoint called...");
}
}