我有以下的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