某天创建一个springboot项目练练手,访问接口的时候开始报错:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Dec 29 11:01:11 CST 2022
There was an unexpected error (type=Internal Server Error, status=500).
而且这个报错调试了一下,能进后台断点,只是返回的时候开始报错:
然后百度了很多解决方法,最终解决了这个问题,这篇文章简单总结一下出现这个问题的原因和解决方法。
注意1:页面上显示这个这个报错,原因不唯一,很多方面的原因都可能造成这个问题,我只记录了我实际遇到的。
注意2:这个报错是500,不是“There was an unexpected error (type=Not Found, status=404)”如果是404,请先检查一下启动类的位置对不对。
原因1:注解问题
有的时候写代码会忘了某些注解,比如@ResponseBody,这个时候可能控制台一般会报错:
Exception processing template “students/getList”: Error resolving template [students/getList], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [students/getList], template might not exist or might not be accessible by any of the configured Template Resolvers
可以参考下面这篇文章解决:
原因2:数据格式问题
控制台会报错类似下面:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: java.time.LocalDateTime cannot be cast to java.lang.String; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.time.LocalDateTime cannot be cast to java.lang.String (through reference chain: java.util.ArrayList[0]->java.util.HashMap[“update_time”])]
可以参考下面这篇文章进行解决:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:
原因3:xml写错
这个主要是xml文件写错了,一般控制台会给出比较明显的提示,比如下面这样:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '})' at line 3
### The error may exist in file [D:\项目\springboot\app\target\classes\mappers\StudentsMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO `students` (`stu_name`, `stu_sex`, `class_id`, `age`) VALUES ( ?, ?, ?, {age});
### Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '})' at line 3
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '})' at line 3] with root cause
很明显,下面这个地方写错了,要加上#变成 #{age}才行。