What is the equivalent of @Context UriInfo in Spring Rest
Asked Answered
F

4

16

I have worked in Jersey and RESTEasy framework earlier and now we will be using Spring Rest for a new project , I don't want to pass all the query params and matrix params as parameters in the method , and usually I would annotate the method with @Context UriInfo and would get all the parameters inside my method in Jersey or RESTEasy Framework for complex parameters.

I would like to know if there is any @Context UriInfo in Spring REST, which is similar to RESTEasy or Jersey Framework. I would like to get all the query params or matrix params and other params if any inside the method instead of passing them as a parameter in the method.

Flatways answered 9/1, 2016 at 5:40 Comment(0)
I
5

I did not find any spring class equivalent to UriInfo. But we can take same info from httpservlet request. Suppose, a url is http:localhost:8080/services/test?one=1&two=2, then,

    hsr.getServletContext.getContextPath() gives "/services"
    hsr.getRequestURI() gives "/services/test"
    hsr.getRequestURL() gives complete url "http:localhost:8080/services/test"
    hsr.getQueryString() gives "one=1&two=2"
    hsr.getServletPath() gives "/test"
    hsr.getParameterMap() gives all query strings in a Map as key value pair

You can set and use these values in URIinfo object

Ignescent answered 11/1, 2016 at 11:25 Comment(1)
If the request mapping use the path like this '/api/user/{id}', how can i get the '/api/user/' only, ignore the '{id}' PathVariableSigmund
B
2

I think there isn't really a similar Api. The options are either using ServletContext like already mentioned, or the following:

RequestAttributes reqAttributes =  RequestContextHolder.currentRequestAttributes();
reqAttributes.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, 0);

This way is useful if you want to get the declared path, that is, with the PathVariables declaration. It won't show the actual values for the @PathVars.

This was already answered here: https://mcmap.net/q/751460/-spring-rest-get-url-path-for-multiple-mapped-endpoint

Bunton answered 11/8, 2022 at 15:23 Comment(0)
H
1

I also did not find any spring class equivalent to UriInfo. I am using as code below:

private getRequstUrl(HttpServletRequest request) {
    String requestUrl = request.getScheme() + "://" + request.getServerName()
    + ("http".equals(request.getScheme()) && request.getServerPort() == 80 || "https".equals(request.getScheme()) && 
    request.getServerPort() == 443 ? "" : ":" + request.getServerPort())
    + request.getRequestURI();
    return requestUrl;
}

public String constructLink(ParamModel paramModel, String requestUrl) {
    StringBuilder stringBuilder = new StringBuilder("<");
    stringBuilder.append(requestUrl);
    if (paramModel.getSize() > 0 && paramModel.getStart() > -1) {
        stringBuilder.append("?");
        stringBuilder.append("start=");
        stringBuilder.append(paramModel.getStart() + paramModel.getSize());

        stringBuilder.append("&");

        stringBuilder.append("size=");
        stringBuilder.append(paramModel.getSize());
    }
    stringBuilder.append(">; rel=\"next\"");
    return stringBuilder.toString();
}
Heda answered 9/9, 2018 at 10:57 Comment(0)
P
0

If you are using Spring MVC you can also access it with:

@Autowired
ServletContext servletContext;

However, it will give you a more limited set of available methods than Kaliappan's approach.

Purveyor answered 18/10, 2016 at 19:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.