繁体   English   中英

Jersey JAX-RS-匹配正则表达式

[英]Jersey JAX-RS - matching regular expression

我有以下资源:

回合资源

@Path("/rounds")
public class RoundResource {
  RoundService roundService = new RoundService();

    @Path("{roundUri}/matches")
    public MatchResource getMatchResource() {
        return new MatchResource();
    }
}

团队资源

@Path("/teams")
public class TeamResource {
    TeamService teamService = new TeamService();

    @Path("/{teamUri}/matches")
    public MatchResource getMatchResource() {
        return new MatchResource();
    }
}

匹配资源

@Path("/matches")
public class MatchResource {
    private MatchService matchService = new MatchService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Match> getTeamMatches(@PathParam("teamUri") String teamUri) {
        return matchService.getTeamMatches(teamUri);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Match> getRoundMatches(@PathParam("roundUri") String roundUri) {
        return matchService.getRoundMatches(roundUri);
    }
}

哪里

/rounds/{roundUri}/matches 

获得一轮比赛

/teams/{teamUri}/matches 

获得球队的比赛

由于匹配资源中的路径为/ matches并且两个路径均以其结尾,因此出现匹配的正则表达式错误。 怎么解决呢?

问题出在您的比赛资源中。 这两种方法映射到相同的网址i,e

/ matches因此,如果您没有给方法提供任何@Path注释,则只有一种方法可以避免冲突,或者为其中一个或两个方法都提供@Path注释。 它会工作

我不明白您为什么在问题中提到正则表达式。 当您要问的问题与正则表达式无关时。

无论如何要给出路径参数的模式,您可以添加如下代码

@Path("method1/{parameter1: [a-zA-Z][a-zA-Z_0-9]*}") // this regular  expression is for alphanumeric

您应该在方法上同时使用@Path注释和在参数上使用@PathParam注释才能正常工作

您应该在您的匹配资源中编写这样的代码

@Path("{teamUri}")

@Path("/roundUri/{roundUri}") 

根据您的工作方式

这将适用于/matches/xxxdf/matches/roundUri/xalfo

像这样更改您的代码,它将起作用

@Path("/matches")
public class MatchResource {
private MatchService matchService = new MatchService();

@GET
@Path("{teamUri}")
 //or  @Path("{teamUri:[a-zA-Z][a-zA-Z_0-9]*}") if you want to use regular expressions
@Produces(MediaType.APPLICATION_JSON)
public List<Match> getTeamMatches(@PathParam("teamUri") String teamUri) {
    return matchService.getTeamMatches(teamUri);
}

@GET
@Path("roundUri/{roudUri}")
  //or  @Path("{roundUri:[a-zA-Z][a-zA-Z_0-9]*}") if you want to use regular expressions
@Produces(MediaType.APPLICATION_JSON)
public List<Match> getRoundMatches(@PathParam("roundUri") String roundUri) {
    return matchService.getRoundMatches(roundUri);
}
}

重点是您的资源网址不应冲突

我在比赛资源方法中添加了@Path(“ OPTION ”)批注,以指定我是否要按轮比赛或按队比赛:

回合资源

@Path("/rounds")
public class RoundResource {
  RoundService roundService = new RoundService();

    @Path("{roundUri}/matches")
    public MatchResource getMatchResource() {
        return new MatchResource();
    }
}

团队资源

@Path("/teams")
public class TeamResource {
    TeamService teamService = new TeamService();

    @Path("/{teamUri}/matches")
    public MatchResource getMatchResource() {
        return new MatchResource();
    }
}

匹配资源

@Path("/matches")
public class MatchResource {
    private MatchService matchService = new MatchService();

    @GET
    @Path("/byteam")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Match> getTeamMatches(@PathParam("teamUri") String teamUri) {
        return matchService.getTeamMatches(teamUri);
    }

    @GET
    @Path("/byround")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Match> getRoundMatches(@PathParam("roundUri") String roundUri) {
        return matchService.getRoundMatches(roundUri);
    }
}

我现在有:

/rounds/{roundUri}/matches/byround 

获得一轮比赛

/teams/{teamUri}/matches/byteam 

获得球队的比赛

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM