java的球衣得到完整的URL

问题描述 投票:10回答:4

我需要做的与新泽西州代理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&param2=value2
}

我该怎么做?

java url jersey
4个回答
14
投票

在新泽西州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();
}

4
投票

如果你需要一个智能代理,你可以得到的参数,筛选它们,并创建一个新的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 + "&"; 
    }

}

3
投票

尝试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

你可以尝试以下选项也

https://localhost:9091/api/suggestionandfeedback/add


0
投票

您可以使用过滤器新泽西州。

enter image description here

之后,美必须以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...");

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.