BeanCreationExceptionとは?
公式API
BeanCreationExceptionとは、公式APIによると、
「Bean 定義から Bean を作成しようとしたときに、BeanFactory でエラーが発生した場合にスローされる例外。」
である。
つまり、DIしたいコンポーネントが何らかの理由で生成できなかった時に発生します。
エラー例
2021-02-09 23:27:39.656 WARN 12596 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/v1/users' method
com.springhack.happylibrary.controller.usr.UserController#get()
to {GET []}: There is already '/v1/books' bean method
com.springhack.happylibrary.controller.bok.BookingController#get() mapped.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-09 23:27:39.719 ERROR 12596 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/v1/users' method
com.springhack.happylibrary.controller.usr.UserController#get()
to {GET []}: There is already '/v1/books' bean method
com.springhack.happylibrary.controller.bok.BookingController#get() mapped.
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.4.2.jar:2.4.2]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/v1/users' method
com.springhack.happylibrary.controller.usr.UserController#get()
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/v1/users' method
発生ケース1
同じURLに対して、複数のControllerが割り当てられている。
解決策
コントローラーに付与した、@RequestMapping/@GetMapping/@PostMapping/@DeleteMappingのvalue値に重複がないか確認する。