Reputation: 28397
I am using SpringBoot and Spring MVC for my Web Application and when I submit any form my controller gets the information encoded with ISO-8859-1 instead of UTF-8.
My application.properties
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
I also have this ServerInitializer class:
@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;
}
}
Thanks for the help.
Upvotes: 1
Views: 5063
Reputation: 11
for Example
@RequestMapping(value="/user", **produces="text/html;charset=UTF-8"**)
public class UserController{
}
add blackbody part is ok!
Upvotes: 1
Reputation: 20777
Are you using 1.3.0.M5 by chance? In that case there's a bug and you might want to consider using the workaround: https://github.com/spring-projects/spring-boot/issues/3912
@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;
}
Upvotes: 6