首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpringBoot - UTF8和控制器

SpringBoot - UTF8和控制器
EN

Stack Overflow用户
提问于 2015-02-08 22:39:19
回答 2查看 4.8K关注 0票数 1

我在我的网络应用程序中使用了SpringBoot和Spring,当我提交任何表单时,我的控制器将获得使用ISO-8859-1而不是UTF-8编码的信息。

我的application.properties

代码语言:javascript
运行
复制
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost/pfg
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.sqlScriptEncoding=UTF-8
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

我还有这个ServerInitializer类:

代码语言:javascript
运行
复制
@Configuration
public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(PfgApplication.class);
    }


    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    CharacterEncodingFilter characterEncodingFilter() {
      CharacterEncodingFilter filter = new CharacterEncodingFilter();
      filter.setEncoding("UTF-8");
      filter.setForceEncoding(true);
      return filter;
    }



}

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-21 14:14:13

你是偶然使用1.3.0M5吗?在这种情况下,存在一个bug,您可能需要考虑使用解决方案:https://github.com/spring-projects/spring-boot/issues/3912

代码语言:javascript
运行
复制
@Autowired
private HttpEncodingProperties httpEncodingProperties;
@Bean
public OrderedCharacterEncodingFilter characterEncodingFilter() {
  OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
  filter.setEncoding(this.httpEncodingProperties.getCharset().name());
  filter.setForceEncoding(this.httpEncodingProperties.isForce());
  filter.setOrder(Ordered.HIGHEST_PRECEDENCE);
  return filter;
}
票数 6
EN

Stack Overflow用户

发布于 2017-05-04 07:54:32

例如

代码语言:javascript
运行
复制
@RequestMapping(value="/user", **produces="text/html;charset=UTF-8"**)

public class UserController{
}        

加黑体部分可以!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28400210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档