Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

java - JAX-RS @PathParam How to pass a String with slashes, hyphens & equals too

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how you enable slashes in path params:

@Path("{inchiname : .+}")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...