VSCodeを利用したSpring bootの環境構築で404エラー

VSCodeを利用したSpring Bootの環境構築について勉強しています。
そこで、@Controllerを利用してindex.htmlファイルの表示をしようと試みております。
ですが、以下のエラーメッセージが表示され期待する動作が得られません。

web上の色々なサイトを調べても解決ができなかったため、ご助言いただけますと幸いです。
不足している情報などがあれば返信や追記させていただきますので、お手数ですがご指摘ください。


発生している問題・エラーメッセージ

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Dec 06 19:16:24 JST 2023 There was an unexpected error (type=Not Found, status=404).

ファイル構造

src └─main ├─java │ └─com │ └─example │ └─spring_demo │ │ SpringDemoApplication.java │ │ │ └─controller │ HelloController.java │ └─resources │ application.properties │ ├─static └─templates index.html

該当のソースコード

SpringDemoApplication.java

package com.example.spring_demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } }

HelloController.java

package com.example.spring_demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("/") public String start() { return "index";   } }

index.html

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ハロワ</title> </head> <body> <h1>Hello World</h1> </body> </html>

実行ログ

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.2.0) 2023-12-06T18:47:12.334+09:00 INFO 22656 --- [ main] c.e.spring_demo.SpringDemoApplication : Starting SpringDemoApplication using Java 21.0.1 with PID 22656 (C:\programs\Java\spring_demo\target\classes started by xxxx in C:\program\Java\spring_demo) 2023-12-06T18:47:12.337+09:00 INFO 22656 --- [ main] c.e.spring_demo.SpringDemoApplication : No active profile set, falling back to 1 default profile: "default" 2023-12-06T18:47:13.664+09:00 INFO 22656 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2023-12-06T18:47:13.671+09:00 INFO 22656 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-12-06T18:47:13.671+09:00 INFO 22656 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.16] 2023-12-06T18:47:13.715+09:00 INFO 22656 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-12-06T18:47:13.715+09:00 INFO 22656 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1318 ms 2023-12-06T18:47:14.462+09:00 INFO 22656 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator' 2023-12-06T18:47:14.542+09:00 INFO 22656 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '' 2023-12-06T18:47:14.554+09:00 INFO 22656 --- [ main] c.e.spring_demo.SpringDemoApplication : Started SpringDemoApplication in 2.524 seconds (process running for 2.835) 2023-12-06T18:47:15.792+09:00 INFO 22656 --- [on(1)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2023-12-06T18:47:15.792+09:00 INFO 22656 --- [on(1)-127.0.0.1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2023-12-06T18:47:15.793+09:00 INFO 22656 --- [on(1)-127.0.0.1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

■使用した拡張機能

  • Java Extension Pack
  • Spring Boot Extension Pack

■構築内容

  • project type:Spring Boot
  • build:Maven
  • spring version:3.2.0
  • language:java
  • packaging type:jar

■アクセス先
localhost:8080/

試したこと

  • エラー内容からコンポーネントスキャンの問題だと判断し、コントローラーファイルの位置を変更したり、@ComponentScanを指定した実行を試したが、同様のエラー結果であった。

  • 新規にプロジェクトを作成し直したりもしたが結果変わらず。

  • @Controllerではなく、@RestControllerを利用した直打ちの文字列などは表示できました。

コメントを投稿

0 コメント