🎃

SpringBootアノテーション書き忘れた場合どうなるか

2021/12/31に公開約3,600字

アノテーションを書き忘れた場合、vscodeではエラーにならないので、
そのまま実行した場合、どのような動作になるのか試してみました
今後のエラー対応がスムーズになればいいなと思います

正常の場合の全体は下記の記事に載せてます

https://zenn.dev/marumarumeruru/articles/13d5c6ab7819a9

@SpringBootApplicationを書き忘れた場合

// @SpringBootApplication
public class RestserviceApplication {
STDOUT
2021-12-31 20:02:28.565 ERROR 71454 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[spring-context-5.3.14.jar:5.3.14]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.2.jar:2.6.2]
        at com.example.restservice.RestserviceApplication.main(RestserviceApplication.java:19) ~[main/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.6.2.jar:2.6.2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160) ~[spring-boot-2.6.2.jar:2.6.2]
        ... 8 common frames omitted


> Task :bootRun FAILED

FAILURE: Build failed with an exception.

ServletWebServerFactory beanがないためとはっきり理由が出る

呼び出し先の@Beanを書き忘れた場合

	// @Bean
	public RestTemplate restTemplate(RestTemplateBuilder builder){

STDOUT
Description:

Parameter 0 of method run in com.example.restservice.RestserviceApplication required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.


> Task :bootRun FAILED

FAILURE: Build failed with an exception.

RestTemplateが見つからないエラーとなる

実行する本体の@Beanを書き忘れた場合

	// @Bean
	public CommandLineRunner run (RestTemplate restTemplate) throws Exception{
STDOUT
2021-12-31 20:18:24.538  INFO 71687 --- [           main] c.e.restservice.RestserviceApplication   : Started RestserviceApplication in 8.026 seconds (JVM running for 8.735)
<==========---> 80% EXECUTING [1m 20s]
> :bootRun

起動してずっとそのまま。特にエラーは出ない。

Discussion

ログインするとコメントできます