加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 05:07:44 所属栏目:Java 来源:网络整理
导读:我在API开发中使用RESTEasy.我的网址是http:// localhost:8080 / project / player / M或http:// localhost:8080 / project / player 这意味着我将{gender}视为路径参数. 我的问题是如何将这个url mapp到REST方法,我使用下面的映射 @GET@Path("player/{g
我在API开发中使用RESTEasy.我的网址是http:// localhost:8080 / project / player / M或http:// localhost:8080 / project / player

这意味着我将{gender}视为路径参数.

我的问题是如何将这个url mapp到REST方法,我使用下面的映射

@GET
@Path("player/{gender}")
@Produces("application/json")

但如果使用它,它映射为http:// localhost:8080 / project / player / M,但不映射到http:// localhost:8080 / project / player.
我需要一个正则表达式来映射零个或多个路径参数

谢谢.

解决方法

路径参数(@PathParam)不是可选的.如果你想要地图;

> http://localhost:8080/project/player/M
> http://localhost:8080/project/player

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读