Java Spring中的重载控制器方法
我有一个控制器需要使用不同的URL参数表现不同。 像这样的东西:
@RequestMapping(method = RequestMethod.GET) public A getA(@RequestParam int id, @RequestParam String query) { ... } @RequestMapping(method = RequestMethod.GET) public A getA(@RequestParam int id) { ... }
但这似乎不起作用,我得到以下exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map '[controller name]' bean method
应用程序是否有一种方法可以根据URL参数选择方法?
在映射中指出哪些参数应该存在
@RequestMapping(method = RequestMethod.GET, params = {"id", "query"}) public A getA(@RequestParam int id, @RequestParam String query) { ... } @RequestMapping(method = RequestMethod.GET, params = {"id"}) public A getA(@RequestParam int id) { ... }