【问题标题】:Tomcat, JAX-RS (jersey), @PathParam: how to pass two variables containing slashesTomcat,JAX-RS(球衣),@PathParam:如何传递两个包含斜杠的变量
【发布时间】:2021-05-22 04:02:24
【问题描述】:

我正在使用 Jersey Java 和 Tomcat 编写一个 REST 服务。这是我的问题 - 我如何接受两个包含斜杠的 @PathParam 变量?即enrollment/{id}/{name},其中id 可以是i124/af23/asf,名称可以是“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!");
}

我看到了这个问题:Tomcat, JAX-RS, Jersey, @PathParam: how to pass dots and slashes?,它回答了我的部分问题,但它提供了一个包含斜杠的参数的解决方案(我有两个带有斜杠的参数)。

任何帮助将不胜感激!

【问题讨论】:

    标签: web-services rest tomcat jersey slash


    【解决方案1】:

    我相信答案是在发送之前对字符串进行 URLEncoding,然后在方法中对字符串进行 URLDecoding。所以我的方法应该是:

    @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!");
    }
    

    【讨论】:

    • 实际上,我们决定将方法切换为 POST 并使用 FormParam 传递变量。不需要 URLEncoding/URLDecoding,因为变量不在 URL 中。路径只是没有变量的“注册”。
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多