某天創建一個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}才行。
