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. How to solve it?
The problem is in your matches resource . The two methods map to same url 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 . 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
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
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.