★★★ Twitterやってます。フォローして頂けると嬉しいです(*^^*) ★★★
この記事にはプロモーションが含まれていることがあります

SprintbootアプリをJARにしたら動かなかった

IT

久しぶりにSpringbootとThymeleafでWebアプリケーションを作ってみたんですが、GCPに乗せる時にすんなり行かなかったので備忘録にしておきます(たぶんまた忘れた頃にハマりそう)。

スポンサーリンク

SpringbootとThymeleaf でWebアプリケーション

久しぶりにSpringbootThymeleafタイムリーフ)、データベースはMySQLという組み合わせでWebアプリケーションを作った時、サーバで動かそうとしたらちょっとハマって時間ロスったのでメモしておきます。

パソコンにVisual Sturio CodeEclipseMySQLをインストール、Springイニシャライザーを使ってブランクプロジェクトを作成して、Webシステム開発開始!

ローカル上で開発は順調に進み、そろそろJARで固めてサーバーで動かそうとしたところ、いくつかつまづいてしまいましたのでまとめました。

MySQLサーバのタイムゾーン指定

エラーメッセージ

Could not create connection to database server.

java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.25.jar!/:8.0.25]
Caused by: java.time.zone.ZoneRulesException: Unknown time-zone ID: JST

対処方法

application.ymlのDB接続設定のタイムゾーンが間違っています。

昔はJSTでいけたんですが、最近はJST使えないみたいです。

SERVERを指定すると、サーバータイムゾーンとなるようです。これで回避しました。国内リージョンのデータベースサーバだからこれでいいんですが、海外のリージョン設置したデータベースサーバの時はどうなるんでしょうか。

  datasource:
    username: dbuser
    password: "dbuser123"
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/test_database?characterEncoding=UTF-8&serverTimezone=SERVER
    driver-class-name: com.mysql.cj.jdbc.Driver
  thymeleaf:
    mode: HTML

テンプレートの問題

エラーメッセージ

template might not exist or might not be accessible by any of the configured Template Resolvers

ローカルの開発環境上では問題なく動いていたんですが、なにやらテンプレートが参照できないような雰囲気です・・・

Exception processing template "/login": Error resolving template [/login],
template might not exist or might not be accessible by any of the configured Template Resolvers


Servlet.service() for servlet [dispatcherServlet] in context with path [] threw 
exception [Request processing failed; 
nested exception is org.thymeleaf.exceptions.TemplateInputException: 
Error resolving template [/login], template might not exist or might not be accessible 
by any of the configured Template Resolvers] with root cause

対処方法

コントローラークラスをチェックして、次のコードのようにテンプレート名の先頭にスラッシュが付いているものがあったら、先頭のスラッシュを削除してください。

Windowsの開発環境上ではちゃんと動いていたのですが、JARファイルにして動かそうとしたらエラーになってしまいました。

/**
 * ログイン画面の表示
 * @return ログイン画面
 */
@GetMapping(value = "/sample/login")
public String getLogin(Model model) {
	model.addAttribute("loginForm", new LoginForm());
	return "/login";
}

修正後のコードです。

/**
 * ログイン画面の表示
 * @return ログイン画面
 */
@GetMapping(value = "/sample/login")
public String getLogin(Model model) {
	model.addAttribute("loginForm", new LoginForm());
	return "login";
}

コメント

タイトルとURLをコピーしました