質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

2256閲覧

Spring Boot 404エラー コントローラ内でhtmlファイルが認識されない

iijima140

総合スコア9

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Eclipse

Eclipseは、IBM社で開発された統合開発環境のひとつです。2001年11月にオープンソース化されました。 たくさんのプラグインがあり自由に機能を追加をすることができるため、開発ツールにおける共通プラットフォームとして位置づけられています。 Eclipse自体は、Javaで実装されています。

Thymeleaf

Thymeleaf(タイムリーフ)とは、Java用のテンプレートエンジンで、特定のフレームワークに依存せず使用することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2022/11/19 14:18

編集2022/11/19 14:20

前提

SpringBootにてWebアプリを作成しています。
Eclipseからスタータープロジェクトを作成し、動作確認用のApplicationクラス、Controllerクラス、HTMLファイルを作成しました。

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

Controller内のメソッドにてhtmlファイル名をreturnにて返していますが、アプリケーションを立ち上げると404エラーになってしまいます。
System.out.println()よりController内のメソッドを通りreturn文まで読まれていることは確認済みです。
また、関係あるかわかりませんがSpring Security提供の http://localhost:8080/login のビューは表示されています。
パッケージ構成やhtmlファイルの配置場所も問題なさそうです(Applicationクラスよりも子パッケージにControllerを配置、classpath/templates 配下にhtmlファイルを配置)
これらよりどうやら自前のtop.htmlがControllerから認識されていないようです。

エラーメッセージはありませんがログを貼り付けておきます。

2022-11-19 22:51:48.381  INFO 84302 --- [ restartedMain] c.e.urisubtitle.UriSubtitleApplication  : Starting UriSubtitleApplication using Java 11.0.16 on sakuas-Mac2.local with PID xxxxx (ディレクトリ名/bin/main started by ユーザ名 in ディレクトリ名) 2022-11-19 22:51:48.382  INFO 84302 --- [ restartedMain] c.e.urisubtitle.UriSubtitleApplication  : No active profile set, falling back to 1 default profile: "default" 2022-11-19 22:51:48.443  INFO 84302 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2022-11-19 22:51:48.443  INFO 84302 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2022-11-19 22:51:49.600  INFO 84302 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http) 2022-11-19 22:51:49.613  INFO 84302 --- [ restartedMain] o.apache.catalina.core.StandardService  : Starting service [Tomcat] 2022-11-19 22:51:49.614  INFO 84302 --- [ restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68] 2022-11-19 22:51:49.707  INFO 84302 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring embedded WebApplicationContext 2022-11-19 22:51:49.708  INFO 84302 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1265 ms 2022-11-19 22:51:50.145  WARN 84302 --- [ restartedMain] .s.s.UserDetailsServiceAutoConfiguration : Using generated security password: パスワード This generated password is for development use only. Your security configuration must be updated before running your application in production. 2022-11-19 22:51:50.270  INFO 84302 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain  : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@611787f5, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@d64568f, org.springframework.security.web.context.SecurityContextPersistenceFilter@c29513a, org.springframework.security.web.header.HeaderWriterFilter@45396bfc, org.springframework.security.web.csrf.CsrfFilter@138081e1, org.springframework.security.web.authentication.logout.LogoutFilter@990bbf8, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@6bd39e9b, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@6e799e61, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@15c8c8d, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@300b828a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4c2259a1, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1cb60f40, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4910b1da, org.springframework.security.web.session.SessionManagementFilter@203f32da, org.springframework.security.web.access.ExceptionTranslationFilter@72abd3b4, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2785449a] 2022-11-19 22:51:50.325  INFO 84302 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer  : LiveReload server is running on port 35729 2022-11-19 22:51:50.366  INFO 84302 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '' 2022-11-19 22:51:50.381  INFO 84302 --- [ restartedMain] c.e.urisubtitle.UriSubtitleApplication  : Started UriSubtitleApplication in 2.429 seconds (JVM running for 3.437) 2022-11-19 22:51:54.935  INFO 84302 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-11-19 22:51:54.935  INFO 84302 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet  : Initializing Servlet 'dispatcherServlet' 2022-11-19 22:51:54.937  INFO 84302 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet  : Completed initialization in 2 ms through check

該当のソースコード

UriSubtitleApplication.java

1package com.example.urisubtitle; 2import org.springframework.boot.SpringApplication; 3import org.springframework.boot.autoconfigure.SpringBootApplication; 4 5@SpringBootApplication 6public class UriSubtitleApplication { 7 public static void main(String[] args) { 8 SpringApplication.run(UriSubtitleApplication.class, args); 9 } 10}

TestController.java

1package com.example.urisubtitle.controller; 2import org.springframework.stereotype.Controller; 3import org.springframework.web.bind.annotation.RequestMapping; 4 5@Controller 6public class TestController { 7 @RequestMapping("/") 8 public String index() { 9 System.out.println("through check"); 10 return "top"; 11 } 12}

top.html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"> 5<title th:text="トップページ">トップページ</title> 6</head> 7<body> 8 <h1 th:text="TOP">トップ</h1> 9</body> 10</html>

当方初心者で何が原因になりうるのかわかっておりません。
他に貼り付けるべき情報がありましたら貼付いたします。

試したこと

  • Controller内定義メソッドを通っていることを確認(@RequestMappingのURIがあっていることを確認)
  • パッケージ構成やhtmlファイルの配置場所の確認
  • 類似質問の確認 (https://teratail.com/questions/344666)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/11/20 04:29

github 等にあげないと回答がつけれないたぐいかと
guest

回答1

0

自己解決

自己解決しました。みてくださった方ありがとうございます。
application.yml 内のspring.thymeleaf.servlet.content-typeに関する設定項目のタイポが原因でした。
- 正: text/html
- 誤: test/thml

投稿2022/11/20 07:57

iijima140

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問