【问题标题】:Spring Boot UTF-8 encoding issue. I suspect it tries to encode ISO-8859-1 to UTF-8Spring Boot UTF-8 编码问题。我怀疑它试图将 ISO-8859-1 编码为 UTF-8
【发布时间】:2015-09-06 08:50:27
【问题描述】:

我想使用 UTF-8 字符编码,但我在某处读到控制器的默认编码为 ISO-8859-1。

我正在使用带速度的弹簧靴。

所以我做了什么,我尝试将以下内容(一次一个)添加到标题中(它们都不起作用。)

<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

在 application.properties 中添加以下几行:

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.velocity.charset=UTF-8
spring.velocity.content-type=text/html

server.tomcat.uri-encoding = UTF-8

我什至尝试在控制器中添加以下行:

@RequestMapping(value = "/", method = RequestMethod.GET, produces={"text/html; charset=UTF-8"})

Plus 尝试将以下 bean 添加到应用程序类中:

 @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        HttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

这是我在速度模板中包含的示例文本:

A sötét lovag igazi főhőse azonban valahogy ezúttal mégsem a mostanság nőnemű családtagjait szállodaszobábkban riogató Christian Bale, azaz a denevérember lett - hanem az ellenfél.

这就是我得到的输出:

A sötét lovag igazi fÅhÅse azonban valahogy ezúttal mégsem a mostanság nÅnemű családtagjait szállodaszobábkban riogató Christian Bale, azaz a denevérember lett - hanem az ellenfél.

编辑:

这是我目前使用的控制器:

@RequestMapping(value = "/", method = RequestMethod.GET, produces={"text/html; charset=UTF-8"})
    public String homepage(Map<String, Object> model) {
        return "homepage";
    }

我在模板中有一个 homepage.vm 文件。其中有一个包含此行的标题部分:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

【问题讨论】:

  • 您确定您用于编辑和保存模板的文本编辑器或 IDE 使用 UTF8 吗?你用什么做编辑?
  • 你试过用-Dfile.encoding=UTF-8启动应用吗?
  • 我到处都在使用 Intellij 和字符编码的 UTF-8。标志给了我相同的输出。我这样启动应用程序: mvn clean spring-boot:run -Dfile.encoding=UTF-8
  • 还有其他想法吗?我真的需要一个解决方案。

标签: spring encoding utf-8 character-encoding iso-8859-1


【解决方案1】:

可能是因为默认的 Eclipse 编码?

Window -> Preferences -> General -> Workspace : Text file encoding

【讨论】:

  • 我正在使用 Intellij IDEA,它设置为 UTF-8
【解决方案2】:

为了解决同样的问题,我使用了 VelocityConfigurer 和以下 bean 定义:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
  <property name="resourceLoaderPath" value="/WEB-INF/pages/"/>
  <property name="configLocation" value="/WEB-INF/velocity.properties"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
  <property name="cache" value="false"/>
  <property name="prefix" value=""/>
  <property name="suffix" value=".vm"/>
  <property name="contentType" value="text/html; charset=UTF-8"/>
</bean>

请注意 viewResolver 中设置的 contentType 属性。 当第一次尝试通过 VelocityEngineFactoryBean 提供它时,我后来才意识到我的属性文件被忽略了。

另外,在velocity.properties 中确保你有:

input.encoding=utf-8
output.encoding=utf-8

对于简单的情况,我在这里不需要任何其他东西。

在 web.xml 中:

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

就是这样,我没有使用控制器中的“produces”属性,不需要 http 元,模板文件中没有指定 xml 字符集编码,也没有弄乱 messageconverter。

编辑:

我没有使用 Spring Boot 的游乐场设置,因此现在无法为您测试确切的实现。然而,这里的 web.xml 只定义了一个过滤器,它也可以通过 Spring Boot 以编程方式实现:

import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

@Component
public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //action here
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void destroy() {}

}

我相信 Spring Boot 会自动扫描 @Component 的来源,否则应该将带有过滤器实现的包添加到组件扫描中。

关于过滤器在doFilter()中应该做什么的想法,你可以探索Spring的CharacterEncodingFilter的源代码:https://github.com/spring-projects/spring-framework/blob/v4.2.1.RELEASE/spring-web/src/main/java/org/springframework/web/filter/CharacterEncodingFilter.java 请注意,上述过滤器已经扩展了其他类(例如 OncePerRequestFilter)。

我想您可以改为添加一个返回过滤器实例的方法:

@Bean
public Filter getCharacterEncodingFilter() {
    org.springframework.web.filter.CharacterEncodingFilter characterEncodingFilter = new org.springframework.web.filter.CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    characterEncodingFilter.setForceEncoding(true);
    return characterEncodingFilter;
}

【讨论】:

  • 感谢您的回答。但是我使用的是spring boot,所以我没有web.xml。我只在 application.properties 文件中声明东西。
  • 非常感谢您的帮助,但无法真正实现。所以我玩弄了 spring boot 的样本,发现 freemarker 和 mustache 支持开箱即用的 utf-8。所以我将继续使用 freemarker。
猜你喜欢
  • 2016-10-04
  • 1970-01-01
  • 2012-09-04
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
相关资源
最近更新 更多