簡體   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