Search code examples
web-servicesresttomcatjerseyslash

Tomcat, JAX-RS (jersey), @PathParam: how to pass two variables containing slashes


I'm writing a REST service using Jersey Java and Tomcat. Here's my question - how do I accept two @PathParam variables that include slashes? i.e. enrollment/{id}/{name}, where id can be i124/af23/asf and name can be "bob/thatcher".

@GET 
@Path("enrollment/{id}/{name}")
public String enrollPerson(@PathParam("id") String id, @PathParam("name") String name) {
    System.out.println(name +  " " + id);
    return("Enrolled!");
}

I saw this question: Tomcat, JAX-RS, Jersey, @PathParam: how to pass dots and slashes? which answered part of my question, but it gave the solution for having one parameter which includes slashes (I have two parameters with slashes).

Any help would be appreciated!


Solution

  • I believe the answer is URLEncoding the Strings before sending, then URLDecoding the Strings in the method. So my method should be:

    @GET 
    @Path("enrollment/{id}/{name}")
    public String enrollPerson(@PathParam("id") String id, @PathParam("name") String name) {
        String decodedName = URLDecoder.decode(name, "UTF-8");
        String decodedId = URLDecoder.decode(id, "UTF-8");
        System.out.println(decodedName + " " + decodedId);
        return("Enrolled!");
    }