具有零个或多个路径参数的Resteasy @path

Rom*_*omi 3 java url resteasy

我在API开发中使用RESTEasy.我的网址是http://localhost:8080/project/player/Mhttp://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.我需要一个正则表达式来映射零个或多个路径参数

谢谢.

mrj*_*jmh 8

是否有任何原因必须是路径参数而不是查询字符串?如果将其更改为使用后者,则可以使用@DefaultValue注释.

因此,您的代码将如下所示:

@GET
@Path("player") //example: "/player?gender=F"
@Produces("application/json")
public Whatever myMethod(@QueryParam("gender") @DefaultValue("M") final String gender) {
  // your implementation here
}
Run Code Online (Sandbox Code Playgroud)


Qwe*_*rky 6

路径参数 ( @PathParam) 不是可选的。如果你想映射;

您将需要两种方法。您可以使用方法重载;

@GET
@Path("player/{gender}")
@Produces("application/json")
public Whatever myMethod(@PathParam("gender") final String gender) {
  // your implementation here
}

@GET
@Path("player")
@Produces("application/json")
public Whatever myMethod() {
  return myMethod(null);
}
Run Code Online (Sandbox Code Playgroud)