一只野生的程序猿 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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记