My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot

Introduction

When validating with Spring Boot, the default error message is English. image.png

Meinotto Bienputi. Perapera.

I'm making an app for Japanese people, so I want to make it Japanese. Japanese localization itself is possible by creating a file called ValidationMessages.properties and defining error messages, but the properties file requires Unicode conversion, so it cannot be read live.

org.hibernate.validator.constraints.NotEmpty.message = \u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002

There is also a method to support with an IDE-specific plug-in, but this time I tried a method to accept UTF8 on the Spring Boot side.

By the way, ValidationMessages.properties written in UTF8 can be read with an editor, but when displayed on the screen, the Japanese part is garbled like this. image.png

Postscript About the difference depending on the version

Below, I have received comments on the differences between Spring Boot versions, so I will capture them!

Spring Boot 2.1

In Spring Boot 2.1, it was possible to convert to UTF-8 without doing anything. The JDK version must be 11.

Spring Boot 1.5.3

I tried it with Spring Boot 1.5.3, but it seems that an error occurs when overriding public Validator getValidator (). (The error content is the same as the one displayed in the reference URL) If I set the bean of mvcValidator instead, it worked!

Thank you to everyone who taught me: bow_tone1:

What i did

Override getValidator of WebMvcConfigurerAdapter. It was just.

WebMvcConfig.java

WebMvcConfig.java


public class WebMvcConfig extends WebMvcConfigurerAdapter {

	/*・ ・ ・ Omitting unrelated settings ・ ・ ・*/

	/**
	 *UTF message Validation message-Allow to set with 8
	 */
	@Override
	public Validator getValidator() {
		return validator();
	}

	@Bean
	public LocalValidatorFactoryBean validator() {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource());
		return validator;
	}

	private MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		//You can also change the name and directory of the property file
		messageSource.setBasename("classpath:/ValidationMessages");
		//UTF-Set to 8
 		messageSource.setDefaultEncoding("UTF-8");
		return messageSource;
	}

}

ValidationMessages.properties

ValidationMessages.properties


org.hibernate.validator.constraints.NotEmpty.message=Please enter.

I can read it. You can read it.

result

image.png

Hyahoi: relaxed:

reference

Spring Framework Documentation

end

Recommended Posts

My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
[Java Spring MVC] I want to use DI in my own class
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
I want to control the maximum file size in file upload for each URL in Spring Boot
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
I want to control the default error message of Spring Boot
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
I want to make an ios.android app
Local file download memorandum in Spring Boot
I want to use @Autowired in Servlet
"Teacher, I want to implement a login function in Spring" ① Hello World
I want to send an email in Java.
I want to use arrow notation in Ruby
How to make Spring Boot Docker Image smaller
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
rsync4j --I want to touch rsync in Java.
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
[Xcode] I want to manage images in folders
How to bind to property file in Spring Boot
I want to be eventually even in kotlin
I wanted to gradle spring boot with multi-project
Spring Boot Memorandum
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I want to get the value in Ruby
A memo that I was addicted to when making batch processing with Spring Boot
A site that was easy to understand when I was a beginner when I started learning Spring Boot
I want to ESLint in Maven project. I want to run Node.js without putting it in my environment.
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
I want to use Combine in UIKit as well.
I want to do something like "cls" in Java
I want to embed any TraceId in the log
I checked asynchronous execution of queries in Spring Boot 1.5.9
If you want to separate Spring Boot + Thymeleaf processing
A memorandum of addiction to Spring Boot2 x Doma2
05. I tried to stub the source of Spring Boot
How to create a Spring Boot project in IntelliJ
I want to use a little icon in Rails
I tried to reduce the capacity of Spring Boot
Spring boot Things that beginners tend to have (@ComponentScan)
I tried to make a login function in Java
I tried to make FizzBuzz that is uselessly flexible
I want to define a function in Rails Console
How to use CommandLineRunner in Spring Batch of Spring Boot
I want to stop snake case in table definition
Introduce swagger-ui to REST API implemented in Spring Boot
I want to click a GoogleMap pin in RSpec
I want to Flash Attribute in Spring even if I set a reverse proxy! (do not do)
I tried to make an application in 3 months from inexperienced
How to change application.properties settings at boot time in Spring boot
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to make a specific model of ActiveRecord ReadOnly
I tried to verify this and that of Spring @ Transactional
I just wanted to make a Reactive Property in Java
Sample web application that handles multiple databases in Spring Boot 1.5
How to call and use API in Java (Spring Boot)
I want to put the JDK on my Mac PC
I tried to make a client of RESAS-API in Java