简体   繁体   English

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

[英]Jersey JAX-RS - matching regular expression

I have the following resources: 我有以下资源:

rounds resource 回合资源

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

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

teams resource 团队资源

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

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

matches resource 匹配资源

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

Where 哪里

/rounds/{roundUri}/matches 

gets the matches of a round 获得一轮比赛

/teams/{teamUri}/matches 

gets the matches of a team 获得球队的比赛

I got the matching regular expression error because the path in matches resource is /matches and both paths end with it. 由于匹配资源中的路径为/ matches并且两个路径均以其结尾,因此出现匹配的正则表达式错误。 How to solve it? 怎么解决呢?

The problem is in your matches resource . 问题出在您的比赛资源中。 The two methods map to same url i,e 这两种方法映射到相同的网址i,e

/matches So if you are not giving any @Path annotation to the methods then only one method should be there to avoid collision or give @Path annotation to either one of them or to both of them . / matches因此,如果您没有给方法提供任何@Path注释,则只有一种方法可以避免冲突,或者为其中一个或两个方法都提供@Path注释。 it will work 它会工作

I don't see why you have mentioned regular expression in your question . 我不明白您为什么在问题中提到正则表达式。 When the question you are asking is nothing related to regular expression . 当您要问的问题与正则表达式无关时。

Anyway to give pattern for path parameters you can add code like this 无论如何要给出路径参数的模式,您可以添加如下代码

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

you should have both @Path annotation on method and @PathParam annotation on argument to work properly 您应该在方法上同时使用@Path注释和在参数上使用@PathParam注释才能正常工作

you should code like this in your matches resource 您应该在您的匹配资源中编写这样的代码

@Path("{teamUri}")

and

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

on your methods to work 根据您的工作方式

this will work for urls like /matches/xxxdf and /matches/roundUri/xalfo 这将适用于/matches/xxxdf/matches/roundUri/xalfo

change your code like this it will work 像这样更改您的代码,它将起作用

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

The point here is your url for resources should not clash 重点是您的资源网址不应冲突

I added @Path(" OPTION ") annotation to the match resource methods to specify if I want the matches by round or by team: 我在比赛资源方法中添加了@Path(“ OPTION ”)批注,以指定我是否要按轮比赛或按队比赛:

rounds resource 回合资源

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

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

teams resource 团队资源

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

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

matches resource 匹配资源

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

Now I have: 我现在有:

/rounds/{roundUri}/matches/byround 

gets the matches of a round 获得一轮比赛

/teams/{teamUri}/matches/byteam 

gets the matches of a team 获得球队的比赛

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

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