Spring 问题定位

导读:本篇文章讲解 Spring 问题定位,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

对于spring框架,我们通常不需要关心内部原理,但是可能会用一些稀奇古怪的问题时,就需要定义是不是Spring框架本身的问题。

定位手段

打开日志

通常我们的spring日志都是info或者error级别的,一些细节的问题我们无法判断是哪里出现了问题。
这个时候就需要把日志级别改的更低,比如debug。

        <AsyncRoot level="debug" includeLocation="true" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="service_appender"/>
        </AsyncRoot>

问题案例

Request method ‘POST’ not supported问题。
这个问题在经过常规方法已经找不到问题时,可以调整日志级别,发现保存信息如下。

 {org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:310}-Looking up handler method for path /test/student
 {org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping:320}-Did not find handler method for [/test/student]
 {org.springframework.web.servlet.handler.SimpleUrlHandlerMapping:192}-Matching patterns for request [/test/student] are [/**]
 {org.springframework.web.servlet.handler.SimpleUrlHandlerMapping:226}-URI Template variables for request [/test/student] are {}
 {org.springframework.web.servlet.handler.SimpleUrlHandlerMapping:140}-Mapping [/file1/upload] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@4c4c7d6c]]] and 1 interceptor
 {org.springframework.web.servlet.DispatcherServlet:1044}-Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
  • 通过日志发现Did not find handler method for [/test/student],可以知道没有找到这个方法。
    但是检查过代码,这个方法肯定是有的。
  • 排查application.properties文件,发现配置如下:
server.servlet.context-path=/test
  • 检查springboot版本是1.5.2,没有该配置,这个配置是高版本才有的。
    在这里插入图片描述
    在这里插入图片描述
  • 配置项替换成server.context-path,文件解决。
server.context-path=/test

总结

这个案例是因为springboot版本太低造成的,配置项在低版本不适用。
我们可以通过打开spring的日志,查看更多细节,从而找到问题到底是哪里出现的,细心一点就可以解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100262.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!