JAX-RS @PathParam 如何传递带有斜杠、连字符和符号的字符串也等于

发布于 2024-08-21 21:57:31 字数 1400 浏览 3 评论 0原文

我是 JAX-RS 的新手,我正在尝试使用 Jersey 构建一个简单的 RESTful Web 服务。

我有 2 个问题。请澄清这些:

  1. 我正在尝试让我的简单网络服务像这样的 URL http://localhost:8080/SampleJersey/rest/inchi/InChIName

    InChIName 是一个类似这样的字符串 InChI=1S/C9H8O4/c1-6(10)13-8-5-3-2-4-7(8)9(11)12/h2- 5H ,1H3,(H,11,12)。我如何将其作为 @PathParam 传递,我的意思是普通字符串工作正常,但这里有斜杠、连字符和逗号。我该如何让它忽略这些。我尝试将其放在引号中,但这不起作用。我应该怎么做?

  2. 我需要将该 InChI 传递给另一个 Web 服务,并返回一个 XML 作为输出,并且我希望将该 XML 输出显示为我的 Web 服务的输出。如果我有 @Produces("application/xml") 它会工作吗?

这是我的代码:

@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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏大泽ㄣ 2024-08-28 21:57:31

这是在路径参数中启用斜杠的方法:

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

This is how you enable slashes in path params:

@Path("{inchiname : .+}")
public String get3DCoordinates(@PathParam("inchiname")String inchiName) 
野却迷人 2024-08-28 21:57:31

Tomcat 不接受 URL 中的 %2F:http://tomcat.apache.org/security-6。 html。您可以关闭此行为。

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

作业与我同在 2024-08-28 21:57:31

以下内容应该有效:

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

(这在另一个答案和评论中有所提及,我只是明确地将其放在单独的答案中以使其清楚)

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)

苏璃陌 2024-08-28 21:57:31

参数应为URL 编码。您可以使用 java.net.URLEncoder为此。

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

然后,/ 将被转换为 %2F

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.

且行且努力 2024-08-28 21:57:31

我使用 @QueryParam() 而不是 @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;
    }
}

因此,URL 结构将类似于 http://myrestservice.com/rest/inchi?inchiname= InChIhere

使用 @PathParam,我在 API 中读到它不接受斜杠。我想知道是否可以在 @Path 中使用任何正则表达式来忽略在引号“”中输入的字符串中的所有斜杠。

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" ".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文