[JAVA] Story when moving from Spring Boot 1.5 to 2.1

Introduction

Spring Boot 1 series EOL will be reached on August 1, 2019 So, I updated the version of Spring Boot to 2 series (1.5.2 2.1.5). Make a note of what you did at that time.

Main configuration

What i did

Information gathering

The official migration guide below describes basic migration methods and precautions. Spring Boot 2.0 Migration Guide

I worked while reading the official migration guide, and googled to resolve any unclear points or stumbling points. We also looked at the release notes for major libraries with changing major versions to see if there were any disruptive changes.

1. First, add spring-boot-properties-migrator to dependencies (* Remove when migration is complete)

In Spring Boot 2, some property keys and settings defined in ʻapplication.properties / ʻapplication.yml have changed. If you add spring-boot-properties-migrator to dependencies, a warning log will be output when the application is started if there are properties whose settings are still out of date. It also temporarily works with the old property writing style.

Add the ↓ line to dependencies. (* Remove when the migration is complete)

build.gradle


runtime "org.springframework.boot:spring-boot-properties-migrator"

2. Added Gradle Dependency Management plugin

In the 1st series, the Dependency Management plugin was included in the Spring Boot plugin, but from the 2nd series, the Dependency Management plugin seems to be different. Add the Dependency Management plugin to build.gradle.

  apply plugin: 'org.springframework.boot'
+ apply plugin: 'io.spring.dependency-management' // <--Add this

3. Increase the version of Spring Boot

  etx {
-   springBootVersion = '1.5.2.RELEASE'
+   springBootVersion = '2.1.5.RELEASE'
  }

Increasing the version of Sprin Boot will automatically increase the version of various libraries.

Major changes this time

Also, the RDB connection pool library has changed from Tomcat JDBC to HikariCP.

4. Spring Boot settings

4.1. Gradle settings

The task bootRepackage is gone and is now a task named bootJar

build.gradle


bootJar {
  mainClassName = 'foo.bar.App'
  launchScript()                // <--with bootRapackage`executable = true`If you have set, this is an alternative
}

4.2. Fixed key name when binding property

If you have bound Spring Boot properties to Java with @ConfigurationProperties or @Value, you need to unify the property name writing to "Canonical". See Spring Boot 2.0 Canonical Properties for more information.

Basically it was OK if I made all lowercase letters and deleted "-" and "_"

- @ConfigurationProperties(prefix = "foo-bar.apiKey")
+ @ConfigurationProperties(prefix = "foobar.apikey")
  1. Spring Web MVC

5.1. WebMvcConfigurerAdapter→WebMvcConfigurer

The WebMvcConfigurerAdapter class has been deprecated and changed to the WebMvcConfigurer interface.


- public class WebMvcConfig extends WebMvcConfigurerAdapter {
+ public class WebMvcConfig implements WebMvcConfigurer {

5.2. Controller can no longer automatically map URLs with extensions

@GetMapping("/users")
public List<Users> listUsers() {

I prepared an endpoint like this and accessed it from the front end with a path like GET /users.json, but this is no longer possible. It's OK to access with GET / users normally, so I deleted all .json.

  1. MySQL

It becomes 5.1 8.0

6.1. Renamed MySQL driver class

I searched for com.mysql.jdbc.Driver and replaced it with com.mysql.cj.jdbc.Driver.

  1. JOOQ

It becomes 3.9 3.11. Due to the new version of JOOQ, some corrections have been made. It's a good idea to read through the Breaking changes in The jOOQ Release Note History.

7.1. Increase the version of JOOQ's Gradle plugin

After JOOQ3.11.x, you need to use gradle-jooq-plugin3.x

 plugins {
-  id 'nu.studer.jooq' version '2.0.11'
+  id 'nu.studer.jooq' version '3.0.3'
 }

7.2. Fixed Gradle settings for JOOQ code generation

Fixed because the API package name has changed

  mainDb(sourceSets.main) {
    jdbc {
      ...
    }
    generator {
-     name = 'org.jooq.util.DefaultGenerator'
+     name = 'org.jooq.codegen.DefaultGenerator'
      ...
      database {
-       name = 'org.jooq.util.mysql.MySQLDatabase'
+       name = 'org.jooq.meta.mysql.MySQLDatabase'
        ...

7.3. Regenerate JOOQ

Regenerate the code

7.4. Fixed Cursor # fetchOne () being deprecated

 while (cursor.hasNext()) 
-  Record record = cursor.fetchOne();
+  Record record = cursor.fetchNext();
  1. Flyway

8.1. Property name change

The property name of application.properties / application.yml is now flyway. *spring.flyway. *

application.yml.diff


- flyway:
-   enabled: true
+ spring:
+   flyway:
+     enabled: true

8.2. Set the name of the migration execution history management table

In Flyway 3 and 4, the execution history of Flyway was managed in a table named schema_version, but in 5 series it has been renamed to flyway_schema_history. To continue using schema_version, set the property spring.flyway.table in ʻapplication.properties / ʻapplication.yml.

application.yml


spring:
  flyway:
    table: schema_version

Also, if you are using the Flyway plugin in Gradle, set it there as well.

build.gradle


flyway {
  ...
  table = 'schema_version'
}
  1. Thymeleaf

9.1. Fixing dependent libraries

- implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4'
+ implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5' // <---Spring Security Dialect version upgrade

9.2. Corrected the template layout description method

The layout notation has changed from Thymeleaf 3. reference:

Example

Layout

layout/default.html.diff


  <!DOCTYPE html>
  <html lang="ja"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
  <head>
    <meta charset="utf-8"/>
    <!-- 「{Title of each page} |It becomes a title like "app name"-->
-   <title layout:title-pattern="$CONTENT_TITLE | $DECORATOR_TITLE">app name</title>
+   <title layout:title-pattern="$CONTENT_TITLE | $LAYOUT_TITLE">app name</title>
    ...
  </head>
  <body>
    <!--Embed common HTML-->
-   <div layout:replace="common/header::partial"></div>
+   <div layout:replace="~{common/header::partial}"></div>

    <!--Expand the content of each page here-->
    <div layout:fragment="content"></div>
  </bod>
  </html>
Individual page

sample/index.html.diff


  <!DOCTYPE html>
  <html
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.springframework.org/schema/mvc"
-     layout:decorator="layout/default">
+     layout:decorate="~{layout/default}">
  <head>
    <title>Title of each page</title>
  </head>
  <body>
    <!--Content of each page-->
    <div layout:fragment="content">
      Hello World
    </div>
  </body>
  </html>

10. Connection pool

Since it has changed from Tomcat JDBC to Hikari CP, modify the settings.

The property name is spring.datasource.tomcat.*spring.datasource.hikari.* Will be.

Items that can be set in HikariCP are in com.zaxxer.hikari.HikariConfig It is defined.

trouble shooting

The front end build contents are no longer included when generating the Jar

Originally, I defined a task to build the front end in Gradle and set it to be executed when the jar was created.

build.gradle


jar.dependsOn compileFrontend

However, from Spring Boot 2 series, jar is generated by bootJar task, so I changed it as ↓

build.gradle


bootJar.dependsOn compileFrontend

DBUnit is moss (MySQL)

org.dbunit.database.AmbiguousTableNameException: ACCOUNTS

It seems that an error occurs when a table with the same name exists across multiple databases. I fixed it by adding the query parameter nullCatalogMeansCurrent = true to the JDBC URL. (It seems that the default behavior of the JDBC driver for MySQL has changed)

Moss when SELECTing on the TINYINT (1) field in JOOQ

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Byte

As a policy when using JOOQ, BIT (1) was mapped to Boolean and TINYINT (1) was mapped to Byte. However, after updating to Spring Boot 2, TINYINT (1) was mapped to Boolean and ClassCastException occurred.

I fixed it by adding the query parameter tinyInt1isBit = false to the JDBC URL. MySQL's JDBC driver seems to treat TINYINT (1) as BIT (1) internally, and it seems that it has become Boolean.

IllegalArgumentException when "_" is included in the domain name of the WEB

java.lang.IllegalArgumentException: The character [_] is never valid in a domain name.

It seems that the version of Tomcat has been upgraded and it no longer accepts domain names containing _. (It seems strange that the domain name includes _ as an RFC specification.) → Since _ was included in the subdomain, I changed the subdomain name to deal with it.

Reference: https://stackoverflow.com/questions/53504857/the-character-is-never-valid-in-a-domain-name

Recommended Posts

Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Upgrade spring boot from 1.5 series to 2.0 series
The story of raising Spring Boot from 1.5 series to 2.1 series part2
The story of raising Spring Boot 1.5 series to 2.1 series
Moving from AWS to PaizaCloud
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
A story that stumbled when deploying a web application created with Spring Boot to EC2
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
How to use ModelMapper (Spring boot)
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
Change Spring Boot REST API request / response from CamelCase to SankeCase
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
[Introduction to Spring Boot] Form validation check
Transition from Struts2 to Spring MVC (Controller)
Precautions when migrating from VB6.0 to JAVA
Javaw.exe error when starting Spring Boot (STS)
How to split Spring Boot message file
Add spring boot and gradle to eclipse
Troubleshooting when raising Mastodon from v3.0.x to v3.1.x
How to not start Flyway when running unit tests in Spring Boot
[Reverse lookup] Spring Security (updated from time to time)
Use Thymeleaf text template mode from Spring Boot
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
03. I sent a request from Spring Boot to the zip code search API
How to make Spring Boot Docker Image smaller
Summary of moss when updating from JMockit 1.4 to 1.30
[Spring Boot] How to get properties dynamically from a string contained in a URL
Try to implement login function with Spring Boot
When @Transactional of Spring Boot does not work
Challenge Spring Boot
How to add a classpath in Spring Boot
An introduction to Spring Boot + in-memory data grid
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Touch all Spring "Guides" (updated from time to time)
Annotation notes when writing tests for Spring Boot
Spring Boot Form
How to bind to property file in Spring Boot
Try to automate migration with Spring Boot Flyway
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
Apply Twitter Bootstrap 4 to Spring Boot 2 using Webjars
Spring Boot Memorandum
◆ Get API created by Spring Boot from React
gae + spring boot
[Spring Boot] How to refer to the property file
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase
[Introduction to Spring Boot] Authentication function with Spring Security
Spring Boot --How to set session timeout time
Precautions when converting from decimal number to binary number
From creating a Spring Boot project to running an application with VS Code
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
What I was addicted to when developing a Spring Boot application with VS Code
A site that was easy to understand when I was a beginner when I started learning Spring Boot
Plans to support JDK 11 for Eclipse and Spring Boot