Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jersey @PathParam: how to pass a variable which contains more than one slashes

package com.java4s;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/customers")
public class RestServicePathParamJava4s {
    @GET
    @Path("{name}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(
                @PathParam("name") String name,
                @PathParam("country") String country) {

        String output = "Customer name - "+name+", Country - "+country+"";
        return Response.status(200).entity(output).build();

    }
}

In web.xml I have specified URL pattern as /rest/* and in RestServicePathParamJava4s.java we specified class level @Path as /customers and method level @Path as {name}/{country}

So the final URL should be

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4/USA

and the response should be displayed as

Customer name - Java4, Country - USA

If I give the 2 input given below it is showing an error. How to solve this?

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4:kum77./@.com/USA` 

Here Java4:kum77./@.com this one is one string and it contains forward slash. how to accept this by using @PathParam or whatever I need to use MultivaluedMap. If somebody knows this plz help me. if anyone knows what is MultivaluedMap, give me a simple example?

like image 915
Vinay Kumar Avatar asked Apr 06 '15 13:04

Vinay Kumar


1 Answers

You'll need to use a regex for for the name path expression

@Path("{name: .*}/{country}")

This will allow anything to be in the name template, and the last segment will be the country.

Test

@Path("path")
public class PathParamResource {

    @GET
    @Path("{name: .*}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(@PathParam("name") String name,
                                            @PathParam("country") String country) {

        String output = "Customer name - " + name + ", Country - " + country + "";
        return Response.status(200).entity(output).build();
    }
}

$ curl http://localhost:8080/api/path/Java4:kum77./@.com/USA
Customer name - Java4:kum77./@.com, Country - USA

$ curl http://localhost:8080/api/path/Java4/USA
Customer name - Java4, Country - USA

like image 53
Paul Samsotha Avatar answered Oct 13 '22 03:10

Paul Samsotha