【问题标题】:JAX-RS @PathParam How to pass a String with slashes, hyphens & equals tooJAX-RS @PathParam 如何传递带有斜杠、连字符和等号的字符串
【发布时间】:2010-02-18 19:09:34
【问题描述】:

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

【问题讨论】:

    标签: java rest jersey jax-rs


    【解决方案1】:

    以下应该有效:

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

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

    【讨论】:

    • 非常感谢。它节省了我的时间并完美地解决了我的问题。非常感谢。
    • 你能告诉如何为查询参数实现相同的功能吗?
    • 以我的拙见,这是最简洁准确的答案。
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢yegor——我使用了这种正则表达式定义方法,但发现它只能通过@Path 参数(而不是@PathParam)工作。 @Path("{inchiname: .+}"); [通过 jersey2]
    【解决方案3】:

    参数应该是URL encoded。您可以为此使用java.net.URLEncoder

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

    / 然后将被转换为%2F

    【讨论】:

    • 点击第一个链接。您会看到= 也是一个保留字符,它将被编码为%3D。此外,在测试应用程序中使用main() 执行System.out.println(URLEncoder.encode("=", "UTF-8")); 有点费力;)
    • 谢谢巴鲁斯。我仍然缺少一些东西。如果我在 REST URL localhost:8080/SampleJersey/rest/inchi/"hello-there" 中给出以下内容,则 evrything 会被很好地编码。但是如果我在其中引入斜线,就像 localhost:8080/SampleJersey/rest/inchi/"hello/there" 我会得到资源未找到错误。我如何在此处编码斜杠,如果我想要斜杠不被编码,我该怎么办?
    • @BalusC BalusC,如果 Tomcat、Apache 和 JBoss 不支持 %2f 怎么办?我可以启用它,但我不想(安全原因)。
    • @BalusC: 如何处理包含斜杠 (/) 和空格 (`) ? URLEncoder.encode("a b/1", "UTF-8")` 的路径参数导致@987654335 @ 这对于查询参数是正确的,但对于路径参数,我期望 a%20b%2F1
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我使用@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 中使用任何正则表达式来忽略字符串中将在引号中输入的所有斜杠“”。

      【讨论】:

        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 2022-01-10
        • 2016-11-20
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        相关资源
        最近更新 更多