Spring Boot上で動作するプログラムの文字コードは、通常UTF-8になっている。その詳細については以下のサイトを参照のこと。
https://qiita.com/kazuki43zoo/items/a365d194f5c4df28912f
今回は、HTMLの文字コードをUTF-8からShift_JISに変更してみたので、そのサンプルプログラムを共有する。
前提条件
下記記事の実装が完了していること。
サンプルプログラムの作成
作成したサンプルプログラムの構成は以下の通り。

まずは、全てのJavaソース、HTML、XML、CSS、プロパティファイルについて、文字コードを「Shift_JIS」に変更する。その手順は以下の通り。
1) 編集するファイルの存在するディレクトリを開き、編集するファイルを選択し右クリックし、「SAKURAで開く」を押下する。

2) 「ファイル」メニューから「名前を付けて保存」を押下する。

3) 文字コードセットを「SJIS」に変更し、「保存」ボタンを押下する。

4) 以下のように、上書き確認のダイアログが表示されるので、「はい」ボタンを押下する。

以上を、全てのJavaソース、HTML、XML、CSS、プロパティファイルについて繰り返す。
さらに、STS上でファイルを開くときの文字コードを変更する。その手順は以下の通り。
1) 開くファイルを選択し右クリックし、「プロパティ」を押下する。

2) テキスト・ファイル・エンコードを「Shift_JIS」に変更し、「適用して閉じる」ボタンを押下する。

以上を、全てのJavaソース、HTML、XML、CSS、プロパティファイルについて繰り返す。
その他、前提条件のプログラムから変更したプログラムは、以下の赤枠となる。

pom.xmlの内容は以下の通りで、先頭のxml宣言とpropertiesタグの文字コードを、Shift_JISに変更している。また、mvnコマンドでのビルドが成功するよう、oracleを利用するための設定も変更している。
<?xml version="1.0" encoding="Shift_JIS"?>
<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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>Shift_JIS</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- log4j2を利用するため、Spring BootデフォルトのLogbackを利用しないよう設定 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombokの設定 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- oracleを利用するための設定 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.7.0.0</version>
</dependency>
<!-- mybatisを利用するための設定 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
<!-- log4j2を利用するための設定 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<!-- AOPを利用するための設定 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- log4j2の設定でymlファイルを利用するための設定 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Apache Common JEXLを利用するための設定 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>さらに、application.propertiesの内容は以下の通りで、文字コードをShift_JISに変更する設定を追加している。なお、application.ymlの場合は、日本語のコメントを入れると動かなくなるため、application.propertiesに変更している。
server.port = 8084 # DB接続情報 spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe spring.datasource.username=USER01 spring.datasource.password=USER01 spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver # 一覧画面で1ページに表示する行数 demo.list.pageSize=2 # Thymeleafの文字コード spring.thymeleaf.encoding=Shift_JIS # メッセージの文字コード spring.messages.encoding=Shift_JIS # Tomcatの文字コード server.tomcat.uri-encoding=Shift_JIS # HTTP文字コード spring.http.encoding.charset=Shift_JIS spring.http.encoding.enabled=true spring.http.encoding.force=true spring.http.encoding.force-request=true spring.http.encoding.force-response=true
また、HTMLファイルについては、全て、文字コードをShift_JISに変更している。以下は完了画面のHTMLファイルの例となるが、全てのHTMLファイルについて変更している。
<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="Shift_JIS">
<title>完了画面</title>
</head>
<body>
お申し込みが完了しました。<br/><br/>
<form method="post" th:action="@{/}">
<input type="submit" value="検索画面に戻る" />
</form>
</body>
</html>さらに、UserDataMapper.xmlの内容は以下の通りで、先頭のxml宣言の文字コードを、Shift_JISに変更している。
<?xml version="1.0" encoding="Shift_JIS" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.UserDataMapper">
<resultMap id="userDataResultMap" type="com.example.demo.UserData" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="birthY" property="birthY" jdbcType="VARCHAR" />
<result column="birthM" property="birthM" jdbcType="VARCHAR" />
<result column="birthD" property="birthD" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="memo" property="memo" jdbcType="VARCHAR" />
<result column="sex_value" property="sex_value" jdbcType="VARCHAR" />
</resultMap>
<select id="findBySearchForm" resultMap="userDataResultMap">
SELECT u.id, u.name, u.birth_year as birthY, u.birth_month as birthM
, u.birth_day as birthD, u.sex, u.memo, u.sex_value
FROM
( SELECT
u1.id, u1.name, u1.birth_year, u1.birth_month, u1.birth_day
, u1.sex, u1.memo, m.sex_value
, ROW_NUMBER() OVER (ORDER BY u1.id) AS rn
FROM USER_DATA u1, M_SEX m
WHERE u1.sex = m.sex_cd
<if test="searchForm.searchName != null and searchForm.searchName != ''">
AND u1.name like '%' || #{searchForm.searchName} || '%'
</if>
<if test="searchForm.fromBirthYear != null and searchForm.fromBirthYear != ''">
AND #{searchForm.fromBirthYear} || lpad(#{searchForm.fromBirthMonth}, 2, '0')
|| lpad(#{searchForm.fromBirthDay}, 2, '0')
<= u1.birth_year || lpad(u1.birth_month, 2, '0') || lpad(u1.birth_day, 2, '0')
</if>
<if test="searchForm.toBirthYear != null and searchForm.toBirthYear != ''">
AND u1.birth_year || lpad(u1.birth_month, 2, '0') || lpad(u1.birth_day, 2, '0')
<= #{searchForm.toBirthYear} || lpad(#{searchForm.toBirthMonth}, 2, '0')
|| lpad(#{searchForm.toBirthDay}, 2, '0')
</if>
<if test="searchForm.searchSex != null and searchForm.searchSex != ''">
AND u1.sex = #{searchForm.searchSex}
</if>
ORDER BY u1.id
) u
<if test="pageable != null and pageable.pageSize > 0">
<where>
u.rn between #{pageable.offset} and (#{pageable.offset} + #{pageable.pageSize} - 1)
</where>
</if>
</select>
<select id="findById" resultMap="userDataResultMap">
SELECT id, name, birth_year as birthY, birth_month as birthM
, birth_day as birthD, sex, memo
FROM USER_DATA
WHERE id = #{id}
</select>
<delete id="deleteById" parameterType="java.lang.Long">
DELETE FROM USER_DATA WHERE id = #{id}
</delete>
<insert id="create" parameterType="com.example.demo.UserData">
INSERT INTO USER_DATA ( id, name, birth_year, birth_month
, birth_day, sex, memo )
VALUES (#{id}, #{name}, #{birthY}, #{birthM}
, #{birthD}, #{sex}, #{memo,jdbcType=VARCHAR})
</insert>
<update id="update" parameterType="com.example.demo.UserData">
UPDATE USER_DATA SET name = #{name}, birth_year = #{birthY}
, birth_month = #{birthM}, birth_day = #{birthD}
, sex = #{sex}, memo = #{memo,jdbcType=VARCHAR}
WHERE id = #{id}
</update>
<select id="findMaxId" resultType="long">
SELECT NVL(max(id), 0) FROM USER_DATA
</select>
</mapper>その他のソースコード内容は、以下のサイトを参照のこと。
https://github.com/purin-it/java/tree/master/sts-make-spring-boot-shift-jis/demo
サンプルプログラムのビルドと実行結果
サンプルプログラムのビルドと実行は、mvnコマンドで実施する。その手順は以下の通り。
1) サンプルプログラムのpom.xmlの存在するディレクトリ上に移動し、「mvn clean」コマンドを実行する。

2) 「mvn package」コマンドを実行し、Jarファイルを生成する。

3) 「java -jar target/(生成されたJarファイル)」コマンドを実行し、Spring Bootアプリケーションを起動する。

4) 「http:// (ホスト名):(ポート番号)」とアクセスすると、以下の画面が表示される。

その他の実行結果は、以下の記事の「サンプルプログラムの実行結果」と同等になる。
要点まとめ
- Spring Boot上で動作するプログラムの文字コードをShift_JISに変更するには、ファイルの文字コードを「Shift_JIS」に変更し、HTMLやXMLで指定する文字コードを「Shift_JIS」に変更すると共に、「application.properties」に文字コードをShift_JISに変更する設定を追加する必要がある。





