0
1

AWSで簡易なWebアプリをデプロイ ~ アプリケーション編 ~

Last updated at Posted at 2024-09-14

お疲れ様です.Tomです.

AWSで簡易なWebアプリをデプロイ【アプリケーション編】です.

全体概要編はこちらをご覧ください.

スタックを示した構成図でいうところの赤で囲った枠が今回のトピックになります.

application01.png

ここではアプリケーションの コーディングとビルド に焦点を当てます.
僕自信オブジェクト指向という概念はなんとなくわかるものの,JavaやHTMLの中身を見てすぐにデバッグできるみたいなスキルはないので,調べながらやりました.また実行に関しては サーバ編で触れることにいたします.

アプリの目的としては定義した機能群をソースコードに落とし込み,【ユーザにフリマサービスを提供すること】です.

アプリケーション編でやることの概要

1.GithubにあげていたソースコードをローカルにPull
2.IntelliJはReadyになっているので,コーディング作業 (デバッグ)
3.必要に応じてBuildとRunを繰り返す (IDEの中)
4.ローカルのLinux環境でもビルドしてみる (Fedora)

Hackathon時はいろいろセットアップしたので,なんかプラグインが足りないだの何だので怒られて,最悪頓挫することも考えたのですが,プレーンのまま実行することができたので安心しました.
(Gitの個人アクセス用トークンに関しては出典に載せておきます)(※1)

3で「いや実行すんのかい」と思った方は鋭いですね.ただサーバ環境ではないうえ,デバッグには必要な過程なのでここに含めました.また実際問題,サーバとアプリの線引きは難しいです…

コーディングする動作環境

開発環境(クライアント側)
HW: Lenovo ThinkPad x270 (準自作)
OS: Fedora 39
IDE: IntelliJ Community Edition
JDK: Java 8 (後ほど Java 11に鞍替え)
フレームワーク: Spring Boot
動作環境: Maven, Tomcat
その他必要言語: HTML CSS, SQL

Hackathonでの開発当初はJDK 8を使っていました.しかしサーバ編で示した通り,サーバへのJDKインストール時に考慮不足で11をインストールしてしまったのですが,ソースコードに不具合はなく,アプリは正常に動作しました.こういったものは後方互換性を持っているので良いですね.足並みを揃えるため,このあとローカルの環境もJava 11に変更しました.

ビルドで行ったことの詳細

主に上記の【3】についてで,アプリの実装では以下の検討が必要です.
1.DB指定
プラグイン等は不要で,application.propertiesファイルに定義するだけで大丈夫でした.

2.pom.xmlがあるディレクトリにて,mvnコマンドでビルド
ここでパッケージの依存性を解決し,JARファイルにまとめてビルドしてくれます.下記勉強になりました.

3.アプリの実行
IntelliJ環境であればGUIの「▶」ボタンから実行できますし,Linux環境であればサーバ編に記載のjavaコマンドで実行できます.実行できればブラウザから任意のURLを指定して確認します.私の場合は下記でした.

ブラウザに入力するURL
http://localhost:8080/

Runの仕組みとしては,クライアントの8080番ポートでアプリを実行してリクエストを待っているということらしいです.

起きた不具合とその対処法

1.トップ画面でユーザを作成する際,最後に500番エラーが出てユーザが作成されない
はじめDBの接続エラーが起こっているのかなと思ったが,どうやら違うみたいでした.

Screenshot from 2024-05-04 12-26-05.png

原因: ユーザ作成時刻の変数のアノテーション不足と変数の型がDBのテーブルと一致していなかったため
対策: @CreationTimestampをアカウント用のエンティティに追記.変数の型もStringだったのでTimestampへ変更

2.ビルドエラー (IDE環境内)

その1
21:47:56.323 [main] ERROR org.springframework.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.
(中略)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

原因: 必要なBeanが定義されていなかったため
対策: 新たにコンフィグを用意してそこにBeanを定義

その2
22:14:00.101 [localhost-startStop-1] ERROR org.springframework.boot.web.embedded.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'multipartConfigElement' defined in class path resource [<クラスのパス>/MultiPartConfigure.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.MultipartConfigElement]: Factory method 'multipartConfigElement' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/util/unit/DataSize
May 29, 2024 10:14:00 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]
22:14:00.123 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

これだけではよくわからなかったので更新したファイルはあるわけだし,とりあえずLinux環境で実施してみることにした.

コマンドと出力
$ mvn clean install
(中略)
[ERROR] <クラスのパス>/ProfileService.java:[12,22] package sun.java2d.cmm does not exist
[ERROR] <クラスのパス>/ProfileController.java:[17,22] package sun.java2d.cmm does not exist

原因: どうやら余計なパッケージをimportしていたのがいけなかった模様.
対策: 勝手に非活性になるのでは?とも思ったが,とりあえずコメントアウトして再度実行すればこのエラーは出なくなった.

その3
06:20:05.854 [main] ERROR org.springframework.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.
(中略)
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:204)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)
        at org.springframework.boot.

正直ここでめちゃくちゃハマりました.
原因: mavenのSpring用のプラグインが古かったためのようでした
対策: アップデートをかけてビルド

コマンドと出力
$ mvn versions:display-dependency-updates
(中略)
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-maven-plugin/2.7.11/spring-boot-maven-plugin-2.7.11.pom
(中略)
[INFO] --- versions:2.10.0:display-dependency-updates (default-cli) @ <PJT名> ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.1.0/maven-reporting-api-3.1.0.pom
(中略)
[INFO] artifact org.springframework.boot:spring-boot-maven-plugin: checking for updates from central
[INFO] The following dependencies in pluginManagement of plugins have newer versions:
[INFO]   org.springframework.boot:spring-boot-maven-plugin .... 2.7.11 -> 3.2.5
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  17.766 s
[INFO] Finished at: 2024-05-19T21:16:14+09:00
[INFO] ------------------------------------------------------------------------

やっと行けた…

実装を終えて(所感)

今回,アプリのやり様でかなり右往左往し,見当違いなことをたくさんしたので,この記事が何かの役に立てば幸いです.

アプリチームの会話を盗み聞きできる用になったという点で成長できました.

お読みいただき,ありがとうございました!

出典

(※1)https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1