Spring Bootでアプリケーション作成①

はじめに

来月から仕事でSpring Frameworkを使用することになったので、
勉強のために以下の本を参考にアプリケーションを作成しました。

Spring Data JPAプログラミング入門

Spring Data JPAプログラミング入門

バージョンの違いなどもあって、エラーが発生したりスムーズにいかなかったところもあったので、 解決策などを忘れないように記録しています。

どんなものを作るか

サービスクラスで、テーブルから社員リストを取得して画面に表示させます。

f:id:xxyoko10:20201225184505p:plain

環境

Windows 10
Java 1.8
Spring Framework 5.3.2
STS 4.8.1
JSF 2.3.7

構成

f:id:xxyoko10:20201225190436p:plain

Thymeleafの設定

テンプレートエンジンとしてThymeleafを使用しているので、pom.xmlにThymeleafの
バージョン指定を追加します。

本にはバージョンは3.0.3.RELEASEと記述されていました。

<thymeleaf.version>3.0.3.RELEASE</thymeleaf.version>

ここを本通りに記述すると、エラー指摘されます。
メッセージ【Missing artifact org.thymeleaf:thymeleaf-spring5:jar:3.0.3.RELEASE】

Spring5がサポートしている最新バージョンに変更します。
以下で最新バージョンを確認
f:id:xxyoko10:20201225134102p:plain

The Central Repository Search Engine

最新バージョンである『3.0.12.RELEASE』に変更すると、エラー指摘が消えました。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.spring.data.jpa.example</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>spring-data-jpa</name>
    <description>Example project for Spring Data JPA</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>    
        <java.version>1.8</java.version>
       <thymeleaf.version>3.0.12.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
        <thymeleaf-extras-java8time.version>3.0.0.RELEASE</thymeleaf-extras-java8time.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
            <version>3.0.3.RELEASE</version>
        </dependency>      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>              

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.propertiesの設定

データベースの接続情報をapplication.propertiesに追加します。
アプリケーションを起動したところエラーが発生しました。

spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

spring.datasource.url=jdbc:mysql://localhost:3306/work
spring.datasource.username=[Connection User Name]
spring.datasource.password=[Connection User Password]
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sql-script-encoding=utf-8
spring.datasource.initialization-mode=always

サーバーのタイムゾーンエラーが発生しました。

エラーメッセージ
java.sql.SQLException:The server time zone value '���� (�W����)' is unrecognized or represents more than one time zone.

You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value

if you want to utilize time zone support.


サーバーのタイムゾーンの名前が不明であるみたいなことを指摘されました。 解決方法は【spring.datasource.url=jdbc:mysql://localhost:3306/work】に【?serverTimezone=JST】を追加するそうです。

spring.thymeleaf.mode=HTML

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

spring.datasource.url=jdbc:mysql://localhost:3306/work?serverTimezone=JST
spring.datasource.username=[Connection User Name]
spring.datasource.password=[Connection User Password]
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sql-script-encoding=utf-8
spring.datasource.initialization-mode=always

これでタイムゾーンエラーが解決しました。
参考:SpringBootのMySQL接続で「The server time zone value」エラー - Code Jamjar

  • com.mysql.jdbc.Driver設定でエラー
    タイムゾーンエラー解決後、今度は以下のエラーが発生しました。

    エラーメッセージ
    Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'.

    The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.


    com.mysql.jdbc.Driverドライバーは非推奨で、新しいドライバーはcom.mysql.cj.jdbc.Driverです、と指摘されました。
    com.mysql.cj.jdbc.Driverに修正します。

spring.thymeleaf.mode=HTML

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

spring.datasource.url=jdbc:mysql://localhost:3306/work?serverTimezone=JST
spring.datasource.username=[Connection User Name]
spring.datasource.password=[Connection User Password]
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.sql-script-encoding=utf-8
spring.datasource.initialization-mode=always


再度、アプリケーションを起動するとエラーはなくなりました。

まとめ

pom.xmlとapplication.propertiesを設定して、アプリケーションを正常に起動するところまで出来ました。 次回は、エンティティー、サービス、コントローラー、テンプレートの作成をします。