java - Jersey @PathParam : how to pass a variable which contains more than one slashes

标签 java rest jersey jax-rs jersey-client

package com.java4s;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/customers")
public class RestServicePathParamJava4s {
    @GET
    @Path("{name}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(
                @PathParam("name") String name,
                @PathParam("country") String country) {

        String output = "Customer name - "+name+", Country - "+country+"";
        return Response.status(200).entity(output).build();

    }
}

在 web.xml 中,我将 URL 模式指定为 /rest/*,在 RestServicePathParamJava4s.java 中,我们将类级别 @Path 指定为/customers 和方法级别 @Path{name}/{country}

所以最终的URL应该是

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4/USA

响应应显示为

Customer name - Java4, Country - USA

如果我给出下面给出的 2 输入,它会显示错误。如何解决?

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4:kum77./@.com/USA` 

这里 Java4:kum77./@.com 这是一个字符串,它包含正斜杠。如何通过使用 @PathParam 或任何我需要使用 MultivaluedMap 来接受它。如果有人知道这个请帮助我。如果有人知道什么是MultivaluedMap,请给我一个简单的例子?

最佳答案

您需要为 name 路径表达式使用正则表达式

@Path("{name: .*}/{country}")

这将允许任何内容出现在 name 模板中,最后一部分将是 country

测试

@Path("path")
public class PathParamResource {

    @GET
    @Path("{name: .*}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(@PathParam("name") String name,
                                            @PathParam("country") String country) {

        String output = "Customer name - " + name + ", Country - " + country + "";
        return Response.status(200).entity(output).build();
    }
}

$ curl http://localhost:8080/api/path/Java4:kum77./@.com/USA
Customer name - Java4:kum77./@.com, Country - USA

$ curl http://localhost:8080/api/path/Java4/USA
Customer name - Java4, Country - USA

关于java - Jersey @PathParam : how to pass a variable which contains more than one slashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29472353/

相关文章:

java - Spring RestClient 内容类型不匹配

java - transactionTemplate 不适用于 mybatis,但我不知道为什么

ajax - 使用 Postman 时 Laravel 无法将 Request 识别为 Ajax

REST 架构 - 在模型或 Controller 中处理数据?

java - 使用 Java/Jersey 作为 REST 和 SOAP 工作的 Web 服务

java - 在方法期间创建 bean 的多个实例

java - schtasks : ERROR: Incorrect Start Date

spring - 如何使用 Thymeleaf 模板从 Json 加载数据

android - 我应该使用什么来保护 REST 网络服务中的参数

json - 使用 jersey 客户端将 JSON 响应作为字符串读取