はじめに
本記事はSpringプロジェクト開発時に発生した(させた)エラー集です。
エラーメッセージ読めばすぐに解決する内容ばかりになりそうな予感ですが備忘録として記載してこうと思います。
開発環境
- バージョン
- Java17
- Spring Boot3.0.5
- OS:Ubuntu Desktop
- IDE:Intellij IDEA
- ビルドシステム:Gradle
- テンプレートエンジン:Thymeleaf
Bean登録漏れ
アプリケーション起動時にエラーが発生する。
コンソールログ
Parameter 0 of constructor in 【使う側のFQCN】 required a bean of type '【使われる側のFQCN】' that could not be found.
Action:
Consider defining a bean of type '【使われる側のFQCN】' in your configuration.
解消方法
使われる側のクラスに@Component相当(@Serviceとか)のアノテーションを付与する。
Bean取得漏れ
アプリケーション起動は出来る。また、該当の処理が実行されるまでは正常に動く。
エラー内容はブラウザに表示されたエラー内容を以下に抜粋
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Apr 21 03:41:46 JST 2024
There was an unexpected error (type=Internal Server Error, status=500).
Cannot invoke "【使われる側のFQCN.メソッド名】" because "this.taskService" is null
java.lang.NullPointerException: Cannot invoke "【使われる側のFQCN.メソッド名】" because "this.【使われる側のインスタンスを保持する想定のフィールド名】" is null
結局はBeanを取得できてないのでnullオブジェクトのメソッド呼び出しが原因でNullPointerExceptionが発生している。
原因はコンストラクターインジェクションなのにコンストラクタがないとかフィールドインジェクションするつもりが@Autowired書いてないとか。
テンプレート名のpath記述ミス
404かと思ったが500エラーが出るらしい。
以下ブラウザに表示されたエラー内容を抜粋
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Apr 21 06:26:39 JST 2024
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [【Controllerでreturnしているパス名】], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [【Controllerでreturnしているパス名】], template might not exist or might not be accessible by any of the configured Template Resolvers
解消方法
templates配下からのパスを拡張子無しで記述(Thymeleafの場合のみ?)
ハンドラーメソッドにアノテーションつけ忘れ
こちらは想定通りだが404
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Apr 21 06:30:46 JST 2024
There was an unexpected error (type=Not Found, status=404).
No message available
解消方法
対象のハンドラーメソッドに@GetMappingなど目的に沿ったアノテーションを付与する。