【问题标题】:UTF-8 encoding with Spring Boot 1.1.1Spring Boot 1.1.1 的 UTF-8 编码
【发布时间】:2014-08-31 11:14:42
【问题描述】:

我如何“说”Spring Boot 使用 UTF-8 编码,正确显示和保存德语变音符号? 我们正在使用 Sping-Boot 1.1.1 (Release) 编写 Java-Web 应用程序,并使用 TomCat7 或 Jetty 作为网络服务器。数据库为postgresql或h2,用于测试。

编辑:

我用属性文件试了一下(感谢回答),但没有可见的变化。

数据库也是 UTF-8... 尤其是当我们向 Web 服务器发送 POST-Request 时,问题就来了。 Spring-Request-Handler 已经获得了损坏的编码值。 在下面您可以看到部分代码: (它显示了 Thymeleaf-Template 的 sn-p)

<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>

这是它的请求处理程序:

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

问候 史蒂夫

【问题讨论】:

    标签: utf-8 character-encoding spring-boot


    【解决方案1】:

    这有帮助:

    spring.datasource.url=jdbc:mysql://localhost:3306/securitydb?useUnicode=yes&characterEncoding=UTF-8
    

    【讨论】:

    • server.tomcat.uri-encoding=UTF-8 都有效。一种用于防止数据库损坏,另一种用于防止在提交 UTF 时损坏。
    【解决方案2】:

    什么是错误编码的?请求还是响应? server.tomcat.uri-encoding 正在将 URI 解码切换为 UTF-8(Jetty 已经是这种情况了)。

    但这对请求正文没有任何作用。默认情况下,Spring MVC 使用 ISO-8859-1 解码它(这是 servlet 规范的默认值)。如果您希望使用UTF-8 对其进行解码,则需要在请求中指定正文编码。大多数用户实际上是使用CharacterEncodingFilter 来实现相同的目的(并确保一致性)。

    如果这样可以解决您的问题,请注意 #1182,它旨在为此提供自动配置。

    【讨论】:

      【解决方案3】:

      我认为最新的默认应该是 UTF-8 吗?请参阅server.tomcat.uri-encoding:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties 上的文档。 OTOH,这可能取决于您需要在哪里进行编码(例如,Spring Boot 对您的数据库服务器编码一无所知)。

      【讨论】:

        【解决方案4】:

        您可以将 UTF-8 设置为 spring-boot-maven-plugin 的默认编码。 我创建了 2 个 maven 配置文件以在 UTF-8 中运行 spring boot,并通过 maven profile 设置活动 spring 配置文件:

        <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>
        

        然后运行

        mvn spring-boot:run 用于开发配置文件 (application-dev.properties) 和

        mvn -P prod spring-boot:run 用于 application.properties

        【讨论】:

          猜你喜欢
          • 2018-07-29
          • 2016-09-23
          • 1970-01-01
          • 2011-08-21
          • 1970-01-01
          • 2019-08-01
          • 2017-01-02
          • 2017-04-27
          相关资源
          最近更新 更多