我正在玩Jersey 2.21,我想知道是否有可能有一个"可选"的参数,它可以或不存在于对服务器的请求中.
我想成功访问这两种方法:
http://localhost:8080/my_domain/rest/api/myMethod/1
http://localhost:8080/my_domain/rest/api/myMethod
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在尝试使integer(id)参数成为可选参数.
我已声明myMethod如下:
@GET
@Path("myMethod/{id}")
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
public String myMethod(@PathParam("id") Integer id, @Context HttpHeaders hh)
Run Code Online (Sandbox Code Playgroud)
这有效:
http://localhost:8080/my_domain/rest/api/myMethod/1
Run Code Online (Sandbox Code Playgroud)
这也有效:
http://localhost:8080/my_domain/rest/api/myMethod/
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我不明白为什么.它抛出一个404 Not Found错误:
http://localhost:8080/my_domain/rest/api/myMethod
Run Code Online (Sandbox Code Playgroud)
你能指出我正确的方向来解决这个问题吗?我不喜欢斜线在所有REST方法调用中都是强制性的,并且如果可能的话想要压缩它.
我在API开发中使用RESTEasy.我的网址是http://localhost:8080/project/player/M或http://localhost:8080/project/player
这意味着我将{gender}视为路径参数.
我的问题是如何将此URL映射到REST方法,我使用下面的映射
@GET
@Path("player/{gender}")
@Produces("application/json")
Run Code Online (Sandbox Code Playgroud)
但如果使用它,它会映射http://localhost:8080/project/player/M但不会映射http://localhost:8080/project/player.我需要一个正则表达式来映射零个或多个路径参数
谢谢.