问题如下:
我在某个controller里的某个方法的RequestMapping有如下定义:
@RequestMapping(
value = "query.action",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaTypes.JSON_UTF_8,
method = RequestMethod.POST
)
当我试图从外部访问这个接口,但是没有在request的header里加"content-type:application/json"时, AbstractHandlerMethodMapping返回了另一个controller里定义的方法,它的@RequestMapping是这样定义的:
@RequestMapping(value = "/login.action", method = RequestMethod.POST, produces = MediaTypes.JSON_UTF_8)
我的本意是想让服务器返回404这样的错误,但是通过追踪spring源代码,发现在
AbstractHandlerMethodMapping.getHandlerInternal的方法里返回了一个所谓的"BestMatch"的handler,这段代码如下:
Match bestMatch = matches.get(0);
if (matches.size() > 1) {
Match secondBestMatch = matches.get(1);
if (comparator.compare(bestMatch, secondBestMatch) == 0) {
Method m1 = bestMatch.handlerMethod.getMethod();
Method m2 = secondBestMatch.handlerMethod.getMethod();
throw new IllegalStateException(
"Ambiguous handler methods mapped for HTTP path '" + request.getRequestURL() + "': {" +
m1 + ", " + m2 + "}");
}
}
纠结了好久不知道怎样通过配置来避免这个问题,现在在想是不是要去重写下AbstractHandlerMethodMapping类。