13
8

More than 5 years have passed since last update.

SpringWebの@RequestMappingのマッピング優先順位

Last updated at Posted at 2016-04-15

曖昧度の低い順番に優先される

  1. /hotels/new
  2. /hotels/{hotel}
  3. /hotels/*

変数の少ない順に並び、ワイルドカードを含む場合は一番優先度が低くなる

/users/1/newsというURLにアクセスするとき

@Controller
@RequestMapping(value = "/users")
public class UserController {
    // 一番優先される
    @RequestMapping(value = "/1/news", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String aaa() {
        return "user/news";
    }

    // 2番目
    @RequestMapping(value = "/{id}/news", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String bbb(@PathVariable String id) {
        return "user/news";
    }

    // 3番目
    @RequestMapping(value = "/{id}/{type}", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String ccc(@PathVariable String id) {
        return "user/news";
    }

    // 一番優先されない
    @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String ddd(@PathVariable String id) {
        return "user/news";
    }
}

ドキュメント
http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/util/AntPathMatcher.html

13
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
8