java jersey 获取完整网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19589339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java jersey get full URL
提问by Denys Romaniuk
I need to do a proxy API service with Jersey. I need to have full request URL in jersey method. I don't want to specify all possible parameters.
我需要用 Jersey 做一个代理 API 服务。我需要在 jersey 方法中有完整的请求 URL。我不想指定所有可能的参数。
For example:
例如:
@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
}
How can I do it?
我该怎么做?
回答by Alexmelyon
In Jersey 2.x (note that it uses HttpServletRequest object):
在 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();
}
回答by Azee
If you need a smart proxy, you can get parameters, filter them and create a new url.
如果你需要一个智能代理,你可以获取参数,过滤它们并创建一个新的 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 + "&";
}
}
回答by Buddhika Alwis
try UriInfo as follow ,
尝试 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());
.........
}
OUT PUT:- https://localhost:9091/api/suggestionandfeedback/add
输出:- https://localhost:9091/api/suggestionandfeedback/add
you can try following options also
您也可以尝试以下选项
回答by Hyman Daniel
you can use Jersey filters.
您可以使用泽西过滤器。
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...");
}
}
After that u must register it in http configuration file or just extend ResourceConfig class. This is how u can register it in http config class
之后你必须在 http 配置文件中注册它或者只是扩展 ResourceConfig 类。这是你如何在 http 配置类中注册它
public class HTTPServer {
public static final Logger logger = LoggerFactory.getLogger(HTTPServer.class);
public static void init() {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9191).build();
ResourceConfig config = new ResourceConfig(Endpoints.class, HTTPFilter.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
logger.info("HTTP Server started");
}
}