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

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

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

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

Spring Boot

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

Q&A

0回答

487閲覧

Spring Bootを用いたアプリでサーバーへアクセスができない。

rsas

総合スコア0

Java

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

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Gradle

Gradleは、ビルド自動化ツールです。 ソフトウェアパッケージやドキュメント、 または実際に何か他の種類のプロジェクトの構築、テスト、公開、展開などを自動化が出来ます

Spring Boot

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

0グッド

0クリップ

投稿2022/02/05 06:47

編集2022/02/05 06:48

初めまして。閲覧ありがとうございます。
所属するIT会社の研修においてJava、およびSpring Bootの学習を行っている者です。

現在、Spring Bootの基本を会社提供の教材で学んでおり、課題の中でアプリケーションの出力先であるURLへのアクセスが必要となります。しかしアプリケーションの実行後にアクセスを試みると502エラーが発生して接続することができません。これを解決したいと考えています。

・アクセス先URL(一部伏せます)「https://****/learner」
・アプリケーションの内容:input画面で文字列を入力して実行すると、output画面に遷移し、入力内容が画面上に表示される。

・ビルドツール:gradle
・使用言語:Java
・フレームワーク:Spring Boot

以下の順序でコードを記させていただきます。
・Javaファイル
・ymlファイル
・htmlファイル
・gradleファイル
・gradleメッセージ
・springログメッセージ
なお一部の記述に関しては伏せさせていただくことがあります。ご容赦お願いいたします。

Ex5Controller.java

1//コントローラーを管理するクラスです。 2 3package curriculu.day16.ex5; 4 5import org.springframework.stereotype.Controller; 6import org.springframework.ui.Model; 7import org.springframework.web.bind.annotation.GetMapping; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.PostMapping; 11 12import org.springframework.web.bind.annotation.ResponseBody; 13import org.springframework.beans.factory.annotation.Autowired; 14import org.springframework.context.annotation.Scope; 15 16@ResponseBody 17@Scope("session") 18@RequestMapping("/learner") //この「/learner」にアクセスすることを望んでいます。 19@Controller 20public class Ex5Controller { 21 22 @Autowired 23 private Ex5Form form; 24 25 @GetMapping("/input") 26 public String input(Model model) { 27 System.out.println("input controller"); 28 model.addAttribute("ex5Form", new Ex5Form()); 29 return "input"; 30 } 31 32 @PostMapping("/input") 33 public String postInput(@ModelAttribute Ex5Form ex5Form, 34 Model model) { 35 System.out.println("output controller"); 36 System.out.println(ex5Form.getName()); 37 this.form = ex5Form; 38 return "redirect:output"; 39 } 40 41 @GetMapping("/output") 42 public String getOutput(Model model) { 43 model.addAttribute("name", this.form.getName()); 44 return "output"; 45 } 46}

Ex5Form.java

1 2package curriculu.day16.ex5; 3 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.stereotype.Component; 7import org.springframework.context.annotation.Scope; 8 9@Component 10public class Ex5Form { 11 private String name; 12 13 public String getName() { 14 return name; 15 } 16 17 public void setName(String name) { 18 this.name = name; 19 } 20}

AppConfig.java

1 2package curriculu.day16.ex5; 3 4import org.springframework.context.annotation.ComponentScan; 5import org.springframework.context.annotation.Configuration; 6 7@Configuration 8@ComponentScan(basePackages = "curriculu.day16") 9public class AppConfig { 10}

Main.java

1 2package curriculu.day16; 3 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6import org.springframework.context.ConfigurableApplicationContext; 7import org.springframework.context.ApplicationContext; 8import org.springframework.beans.factory.annotation.Autowired; 9import curriculu.day16.ex5.*; 10 11@SpringBootApplication 12public class Main { 13 14 public static void main(String[] args) { 15 SpringApplication.run(AppConfig.class, args); 16 17 } 18}

springの設定ファイルです。

application.yml

1spring: 2 mvc: 3 view: 4 prefix: \static\ 5 suffix: .html

HTMLファイルです。

input.html

1<!DOCTYPE html> 2<html lang="en" xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Ex5</title> 6</head> 7<body> 8 <form method="post" action="#" th:action="@{/input}" th:object="${ex5Form}"> 9 <input th:field="*{name}" type="text"> 10 <input type="submit" value="output"> 11 </form> 12</body> 13</html>

output.html

1<!DOCTYPE html> 2<html lang="en" xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8"> 5 <title>output</title> 6</head> 7<body> 8 <label th:text="${name}"></label> 9</body> 10</html>

Gradleの設定ファイルです。

build.gradle

1plugins { 2 id 'org.springframework.boot' version '2.4.5' 3 id 'io.spring.dependency-management' version '1.0.11.RELEASE' 4 id 'java' 5} 6 7group = 'curriculu' 8version = '0.0.1-SNAPSHOT' 9sourceCompatibility = '11' 10 11repositories { 12 mavenCentral() 13} 14 15dependencies { 16 implementation 'org.springframework.boot:spring-boot-starter-web' 17 compile 'org.springframework.boot:spring-boot-starter-thymeleaf' 18 testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' 19 testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' 20} 21 22jar { 23 manifest { 24 attributes 'Main-Class': 'Main' 25 } 26}

Gradleの実行時ログメッセージです。

Gradle

1To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/6.8/userguide/gradle_daemon.html#sec:disabling_the_daemon. 2Daemon will be stopped at the end of the build 3> Task :clean 4 5Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. 6Use '--warning-mode all' to show the individual deprecation warnings. 7See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings 8 9BUILD SUCCESSFUL in 6s 101 actionable task: 1 executed 11To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/6.8/userguide/gradle_daemon.html#sec:disabling_the_daemon. 12Daemon will be stopped at the end of the build 13 14> Task :compileJava 15Note: /usr/src/Day16/Task3/day16/src/main/java/curriculu/day16/ex5/MyDetailErrorController.java uses or overrides a deprecated API. 16Note: Recompile with -Xlint:deprecation for details. 17Note: /usr/src/Day16/Task3/day16/src/main/java/curriculu/day16/ex5/MyDetailErrorController.java uses unchecked or unsafe operations. 18Note: Recompile with -Xlint:unchecked for details. 19 20> Task :processResources 21> Task :classes 22> Task :bootJarMainClassName 23> Task :bootJar 24> Task :jar SKIPPED 25> Task :assemble 26> Task :compileTestJava NO-SOURCE 27> Task :processTestResources NO-SOURCE 28> Task :testClasses UP-TO-DATE 29> Task :test NO-SOURCE 30> Task :check UP-TO-DATE 31> Task :build 32 33Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. 34Use '--warning-mode all' to show the individual deprecation warnings. 35See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings 36 37BUILD SUCCESSFUL in 10s 384 actionable tasks: 4 executed 39

最後に、springの実行時ログメッセージです。

Spring

1. ____ _ __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5 ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot :: (v2.4.5) 8 92022-02-05 06:35:58.799 INFO 1431 --- [ main] curriculu.day16.Main : Starting Main using Java 11.0.12 on e2d6b5cf8265 with PID 1431 (/usr/src/Day16/Task3/day16/build/libs/day16-0.0.1-SNAPSHOT.jar started by root in /usr/src/Day16/Task3/day16/build/libs) 102022-02-05 06:35:58.810 INFO 1431 --- [ main] curriculu.day16.Main : No active profile set, falling back to default profiles: default 112022-02-05 06:36:00.700 INFO 1431 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 122022-02-05 06:36:00.717 INFO 1431 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 132022-02-05 06:36:00.717 INFO 1431 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45] 142022-02-05 06:36:00.794 INFO 1431 --- [ main] o.a.c.c.C.[.[localhost].[/learner] : Initializing Spring embedded WebApplicationContext 152022-02-05 06:36:00.794 INFO 1431 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1872 ms 162022-02-05 06:36:01.122 INFO 1431 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 172022-02-05 06:36:01.418 INFO 1431 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/learner' 182022-02-05 06:36:01.441 INFO 1431 --- [ main] curriculu.day16.Main : Started Main in 3.631 seconds (JVM running for 4.542)

長くなりましたが、ご覧いただきありがとうございます。

本件に関して研修作成者に救いを求めた際、Mappingの仕様によるものだろう、とヒントをいただきましたが、いじるべきところがわからず、手が付けられていません。

初心者のため、投稿に関しまして資料の不足がありましたらお申し付けください。
できる限り早くご対応をさせていただきます。

また当方初学者のため、コード上に不可解な記述が多いかと思います。
本件の表題に関係なことであっても、なにか間違いがございましたら、遠慮なくご指摘のほどをお願いいたします。

よろしくお願いいたします。

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

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

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

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

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

hoshi-takanori

2022/02/06 20:10

それだと https://****/learner/input や https://****/learner/output にはアクセスできるのでは。
rsas

2022/02/07 00:27

回答ありがとうございます。 ご指摘の方法でアクセスを試みたところ、アクセスすることが叶いました。 ご指摘誠にありがとうございます。 ただアクセスしてみたところ、ここで記述している肝心のHTMLファイルが反映されていませんでした。 つまり指定したURLにとんでも白紙のページになってしまっている状態です。(404、502は表示されない) 当方でも改善点を探したいと思いますが、 こちらの問題に関して、もし再び助言がいただけましたら幸いです。 よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問