我有以下的Spring MVC控制器:春天請求映射定製註釋 - 曖昧映射
@RestController
@RequestMapping(value = "my-rest-endpoint")
public class MyController {
@GetMapping
public List<MyStuff> defaultGet() {
...
}
@GetMapping(params = {"param1=value1", "param2=value2"})
public MySpecificStuff getSpecific() {
...
}
@GetMapping(params = {"param1=value1", "param2=value3"})
public MySpecificStuff getSpecific2() {
return uiSchemas.getRelatedPartyUi();
}
}
我需要的是讓使用自定義的註釋更通用:
@RestController
@RequestMapping(value = "my-rest-endpoint")
public class MyController {
@GetMapping
public List<MyStuff> defaultGet() {
...
}
@MySpecificMapping(param2 = "value2")
public MySpecificStuff getSpecific() {
...
}
@MySpecificMapping(param2 = "value3")
public MySpecificStuff getSpecific2() {
return uiSchemas.getRelatedPartyUi();
}
}
我知道,春天meta annotations可以幫助我。
所以我定義了註解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@RequestMapping(method = RequestMethod.GET, params = {"param1=value1"})
public @interface MySpecificMapping {
String param2() default "";
}
這本身就不會做的伎倆。
所以我添加一個攔截器來處理與「參數2」:
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// get annotation of the method
MySpecificMapping mySpecificMapping = handlerMethod.getMethodAnnotation(MySpecificMapping.class);
if (mySpecificMapping != null) {
// get the param2 value from the annotation
String param2 = mySpecificMapping.param2();
if (StringUtils.isNotEmpty(param2)) {
// match the query string with annotation
String actualParam2 = request.getParameter("param2");
return param2 .equals(actualParam2);
}
}
}
return true;
}
}
並將其包含到課程的Spring配置。
工作正常,但只有當我有一個這樣的自定義映射每個控制器。
如果我添加與@MySpecificMapping
甚至具有「參數2」不同的值註釋兩種方法然後我得到了應用程序啓動的「曖昧映射」錯誤:
java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'myController' method
public com.nailgun.MySpecificStuff com.nailgun.MyController.getSpecific2()
to {[/my-rest-endpoint],methods=[GET],params=[param1=value1]}: There is already 'myController' bean method
public com.nailgun.MySpecificStuff com.nailgun.MyController.getSpecific() mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576)
- Application startup failed
我明白爲什麼會發生。
但你能幫我給Spring一個暗示,那些是兩個不同的映射嗎?
我使用Spring引導具有Spring Web 4.3.5