Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to get the path parameters in httpservlet request

I have rest service implemented.

I am trying to get the path parameters of the the request in filter.

My request is

/api/test/{id1}/{status}

 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException
    {
         //Way to get the path parameters id1 and status


     }
like image 525
Patan Avatar asked Mar 05 '14 13:03

Patan


People also ask

How do I get all request parameters?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

What are the parameters in HttpServletRequest?

Parameters. The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

How do I request a full URL?

String getRequestURI() -Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. java. lang. StringBuffer getRequestURL() -Reconstructs the URL the client used to make the request.


3 Answers

String pathInfo = request.getPathInfo();
    if (pathInfo != null) {
        String[] parts = pathInfo.split("/");
        int indexOfName = Arrays.asList(parts).indexOf("test");
        if (indexOfName != -1) {
            Optional<String> testId1 = Optional.of(parts[indexOfName + 1]);
            Optional<String> status= Optional.of(parts[indexOfName + 2]);
        }

    }

Your Servlet Mapping should be till /api/* eg. @WebServlet(urlPatterns = {"/api/*"})

like image 128
Saurabh Talreja Avatar answered Oct 18 '22 20:10

Saurabh Talreja


There's no other way to do it in a ServletFilter other than trying to parse the URI yourself, but you can access the path parameters if you decide to use a JAX-RS request filter:

@Provider
public class PathParamterFilter implements ContainerRequestFilter {

    @Override
     public void filter(ContainerRequestContext request) throws IOException {
        MultivaluedMap<String, String> pathParameters = request.getUriInfo().getPathParameters();
        pathParameters.get("status");
        ....
    }
}
like image 43
jkovacs Avatar answered Oct 18 '22 22:10

jkovacs


You can autowire HttpServletRequest in your filter and use it to get information.

@Autowire
HttpServletRequest httpRequest


httpRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)  

will give you map of path params.

Example:

If your request is like url/{requestId} then above map will return

0 = {LinkedHashMap$Entry@12596} "requestId" -> "a5185067-612a-422e-bac6-1f3d3fd20809"
 key = "requestId"
 value = "a5185067-612a-422e-bac6-1f3d3fd20809"
like image 24
Vivek Garg Avatar answered Oct 18 '22 22:10

Vivek Garg