3

How can I "say" Spring Boot to use the UTF-8 encoding, to show and save German umlauts correctly? We are programming a Java-Webapplication using Sping-Boot 1.1.1 (Release) and as webserver a TomCat7 or Jetty. The database is postgresql or h2 for testing.

Edit:

I tried it with the properties file (thanks for the answer), but no changes are visible.

The database is also UTF-8... Especially the problem comes, when we send a POST-Request to the Webserver. The Spring-Request-Handler gets already the broken encoded values. In the following you can see a part of the code: (It shows a snippet of the Thymeleaf-Template)

<form accept-charset="utf-8" method="post">
        <div class="row">
            <fieldset th:object="${model}">
                <!-- CSRF token -->
                <th:block th:replace="makros :: csrf" />
                <div class="col-sm-4 form-group" >
                    <label for="firstname" th:text="#{edit_user.first_name}">Given Name</label>
                    <input class="form-control required" type="text" required="required" id="firstname" name="firstname" th:field="*{firstName}" />
                </div>
                <div class="col-sm-4 form-group">
                    <label for="firstname" th:text="#{edit_user.last_name}">Family Name</label>
                    <input class="form-control required" type="text" required="required" id="lastname" name="lastname" th:field="*{lastName}" />
                </div>
            </fieldset>
        </div>
 </form>

And this is the request handler for that:

@RequestMapping(method = RequestMethod.POST)
public String handleUserUpdate(@ModelAttribute(MODEL) UpdateUserCommand command) {
   //here we cut the broken encoded values
}

Greetings Stef

4 Answers 4

5

This helped:

spring.datasource.url=jdbc:mysql://localhost:3306/securitydb?useUnicode=yes&characterEncoding=UTF-8
Sign up to request clarification or add additional context in comments.

1 Comment

Both this and server.tomcat.uri-encoding=UTF-8 worked. One to prevent corruption in the DB and one to prevent corruption when submitting UTF.
4

What is wrongly encoded? The request or the response? server.tomcat.uri-encoding is switching the URI decoding to UTF-8 (this is already the case for Jetty).

But that does not do anything for the request body. By default, Spring MVC decodes that with ISO-8859-1 (that is the default per the servlet spec). You need to specify a body encoding in your request if you want it to be decoded using UTF-8. Most users are actually using the CharacterEncodingFilter to achieve the same thing (and ensure consistency).

If that fixes your issue, watch out for #1182 that is meant to provide an auto-configuration for that.

Comments

2

The latest should be UTF-8 by default I think? See docs on server.tomcat.uri-encoding: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties. OTOH it might depend on where you need the encoding to happen (Spring Boot knows nothing about your database server encoding for instance).

Comments

0

You can set UTF-8 as default encoding for spring-boot-maven-plugin. I create 2 maven profile to run spring boot in UTF-8 and set active spring profile via maven profile:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <jvmArguments>-Dfile.encoding=UTF8 -Dspring.profiles.active="dev"</jvmArguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

And run

mvn spring-boot:run for dev profile (application-dev.properties) and

mvn -P prod spring-boot:run for application.properties

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.