2

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!

svarog
  • 9,477
  • 4
  • 61
  • 77
LConrad
  • 816
  • 1
  • 11
  • 20

1 Answers1

4

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!");
}
svarog
  • 9,477
  • 4
  • 61
  • 77
LConrad
  • 816
  • 1
  • 11
  • 20
  • Actually, we settled on switching the method to POST and using FormParam to pass the variables. No URLEncoding/URLDecoding needed, since the variables are not in the URL. The Path would just be "enrollment" with no variables. – LConrad Nov 01 '12 at 18:47