最近在学习SpringBoot,在页面访问上总是遇到一些问题,今天抽个时间总结一些遇到的几种情况,希望有所帮助。
GET/POST请求不支持
org.springframework.web.HttpRequestMethodNotSupportedException:
Request method 'GET' /'POST'not supported
1.确认页面与Controller是否使用了不同的请求方式,如果你是用的是RequestMapping,或者页面默认的请求方式,数据发送方式都默认为Get请求,会将数据放在url中发送,
2.确认前后端数据参数名类型等是否一致,不一致的参数类型也会导致请求不支持
3.请求路径是否正确?
4.数据格式问题,你所传递的数据格式是否为Get/Post格式
5.如果你使用了2.x以上版本的springboot且用了Thymeleaf,检查你的pom中是否有过更改版本。以下行为会导致冲突问题
如果你使用了版本增强的话,可以去掉了,在springboot新版本中已经将Thymeleaf默认支持版本提升到3.0.11
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
可以在这里找到springboot的默认版本信息
/spring-boot-dependencies/2.2.6.RELEASE/spring-boot-dependencies-2.2.6.RELEASE.pom:9
博主所使用的SpringBoot的默认Thymeleaf版本信息
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
<thymeleaf-extras-java8time.version>3.0.4.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-extras-springsecurity.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity.version>
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
404页面问题
1.检查你的主函数是否在Controller在同一及目录或父目录下
2.检查Controller中的路径,注解,映射
3.是否使用了Thymeleaf模板,如果使用是否放在了默认的静态资源路径下.
4.有没有在application文件中加入server.servlet.context-path=/xxx
访问前缀
5.Thymeleaf版本冲突会导致你能够进入Controller并执行其中代码,但无法返回正确的页面
6.如果自设了拦截器,检查拦截器是否拦截了你的访问路径