同一个Controller的@RequestMapping指向同一个value会发生什么

1,940 阅读1分钟

首先是这种

@RequestMapping(value="/a")
    public String a() {
        return "a";
    }
    
    @RequestMapping(value="/a")
    public String d() {
        return "a";
    }

这个应该都知道了,肯定会报错的 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. 错误信息也很明确Ambiguous mapping。

那是不是value的值一定不能一样呢,答案是否定的

@RestController
public class TestController {
    
    @RequestMapping(value="/a")
    public String a() {
        return "a";
    }
    
    @RequestMapping(value="/a", method = RequestMethod.HEAD)
    public ResponseEntity<Void> b() {
        return ResponseEntity.ok().header("b", "b").build();
    }
    
    @RequestMapping(value="/a", method = RequestMethod.GET)
    public String c() {
        return "c";
    }
}

1.png

2.png

3.png

从结果可以看出,只要指定不同method就行。而且会优先匹配对应的方法,没有才会执行没有指定method的@RequestMapping对应方法