40

I am new to JAX-RS and I am trying to use Jersey to build a simple RESTful Webservice.

I have 2 questions. Please clarify these:

  1. I am trying to have my simple webservice like this URL http://localhost:8080/SampleJersey/rest/inchi/InChIName

    The InChIName is a string like this InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2- 5H,1H3,(H,11,12). How do I pass this as a @PathParam, I mean a normal String is working fine but here there are slashes,hyphens, and commas. How do I make it to ignore these. I tried putting it in quotes, but that didnt work. How should I do this?

  2. I need to pass that InChI to another webservice and that returns an XML as an output and I want to display that XML output as my Webservice's output. If I have @Produces("application/xml") will it work?

This is my code:

@Path("/inchi")
public class InChIto3D {
    @GET
    @Path("{inchiname}")
    @Produces("application/xml")
    public String get3DCoordinates(@PathParam("inchiname")String inchiName) {
        String ne="";
        try{
            URL eutilsurl = new URL(
                      "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?"
                      + "db=pccompound&term=%22"+inchiName+"%22[inchi]");
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(eutilsurl.openStream()));
            String inputline;
            while ((inputline=in.readLine())!=null)
                ne=ne+inputline;
        }catch (MalformedURLException e1) {
        }catch (IOException e2){
        }
        return ne;
    }
}
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
Sashi Kiran Challa
  • 905
  • 1
  • 11
  • 21

5 Answers5

41

This is how you enable slashes in path params:

@Path("{inchiname : .+}")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) 
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • 2
    Thanks yegor – I used this regex definition approach, but found it only worked via the @Path param (instead of @PathParam). @Path("{inchiname: .+}"); [via jersey2] – ThomasMH Mar 23 '14 at 21:35
19

Tomcat does not Accept %2F in URLs: http://tomcat.apache.org/security-6.html. You can turn off this behavior.

simonox
  • 559
  • 6
  • 6
12

The following should work:

@GET
@Path("{inchiname : (.+)?}")
@Produces("application/xml")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) {

(This is sort of mentioned in another answer and comment, I'm just explicitly putting it in a separate answer to make it clear)

AmanicA
  • 4,659
  • 1
  • 34
  • 49
10

The parameters should be URL encoded. You can use java.net.URLEncoder for this.

String encodedParam = URLEncoder.encode(unencodedParam, "UTF-8");

The / will then be translated to %2F.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Click at the 1st link. You'll see that `=` is a reserved character as well and that it will be encoded to `%3D`. Also, it's a little of effort to just do a `System.out.println(URLEncoder.encode("=", "UTF-8"));` in a test application with a `main()` ;) – BalusC Feb 20 '10 at 17:36
  • 2
    Thanks Balus. I still am missing something. If I give the following in the REST URL http://localhost:8080/SampleJersey/rest/inchi/"hello-there" evrything gets encoded fine. But if I introduce the slash in there like this http://localhost:8080/SampleJersey/rest/inchi/"hello/there" I get resource not found error. How do I get the slashes encoded here, & if I want the slashes as such without being encoded what do I do? – Sashi Kiran Challa Feb 20 '10 at 18:52
  • @BalusC BalusC, what should I do if Tomcat, Apache and JBoss don't support %2f ? I can enable it, but I don't want to (security reasons). – Oleg Pavliv May 19 '14 at 14:51
  • @BalusC: how to handle pathparams containing slash (`/`) and space (` `) ? `URLEncoder.encode("a b/1", "UTF-8")` results in `a+b%2F1` which is correct for a query param but for path param I expected `a%20b%2F1`. – raho Jun 15 '18 at 09:43
3

I got this to work using @QueryParam() rather than @PathParam.

@Path("/inchi")
public class InChIto3D {
    @GET
    //@Path("{inchiname}")
    @Produces("application/xml")
    public String get3DCoordinates(@QueryParam("inchiname") String inchiName) {
          String ne="";
          try{
              URL eutilsurl = new URL(
                      "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?"
                      + "db=pccompound&term=%22"+inchiName+"%22[inchi]");
              BufferedReader in = new BufferedReader(
                                new InputStreamReader(eutilsurl.openStream()));
             String inputline;
             while ((inputline=in.readLine())!=null)
                 ne=ne+inputline;
          }catch (MalformedURLException e1) {
          }catch (IOException e2){
          }
          return ne;
    }
}

Thus the URL structure would be like this http://myrestservice.com/rest/inchi?inchiname=InChIhere

With @PathParam I read in the API that it will not accept slashes. I am wondering if I can use any Regular Expression in @Path just to ignore all slashes in the string that would be entered in quotes" ".

Sashi Kiran Challa
  • 905
  • 1
  • 11
  • 21