一只野生的程序猿 2017-05-31 09:48 采纳率: 0%
浏览 1397
已结题

Spring getHandlerInternal 返回错误handler问题

问题如下:
我在某个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类。

  • 写回答

2条回答 默认 最新

  • lihongbiao0610 2017-05-31 11:01
    关注

    你的意思是你访问query.action跳转到了login.action 最后出现了上面的错误吗

    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作