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/