我创建了一个Spring Boot应用程序,它在本地主机上运行得非常好,但是我无法将Cyrillic字母保存到数据库中。Hibernate查询正在尝试保存一个[?????????]字符串到数据库,所以它在到达数据库之前被转换。在我的数据库中,西里尔文可以正常工作。所有的表和列都是utf8mb4_unicode_ci。
我已经尝试过这个:utf8字符集与百里香Мумин回答。但是我看西里尔字母没有问题,只是保存它们有问题。
在my application.properties中:
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
My EncodingConfig类:
package util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
@Configuration
public class EncodingConfig
{
private @Autowired AutowireCapableBeanFactory beanFactory;
public FilterRegistrationBean<CharacterEncodingFilter> filterRegistrationBean()
{
FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<CharacterEncodingFilter>();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
beanFactory.autowireBean(characterEncodingFilter);
characterEncodingFilter.setForceEncoding(true);
characterEncodingFilter.setEncoding("UTF-8");
registrationBean.setFilter(characterEncodingFilter);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
}
MVCConfig类:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.Companyname.Controllers")
public class MVCConfig implements WebMvcConfigurer, ApplicationContextAware
{
@Autowired
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver viewResolver()
{
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine((ISpringTemplateEngine) templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
return thymeleafViewResolver;
}
@Bean
public TemplateEngine templateEngine()
{
SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
springTemplateEngine.setEnableSpringELCompiler(true);
springTemplateEngine.setTemplateResolver(templateResolver());
return springTemplateEngine;
}
@Bean
public ITemplateResolver templateResolver()
{
SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
springResourceTemplateResolver.setApplicationContext(applicationContext);
springResourceTemplateResolver.setPrefix("/WEB-INF/views/");
springResourceTemplateResolver.setTemplateMode(TemplateMode.HTML);
springResourceTemplateResolver.setSuffix(".html");
springResourceTemplateResolver.setCharacterEncoding("UTF-8");
return springResourceTemplateResolver;
}
}
我已经放入了pom文件:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
我的控制器方法在@RequestMapping注释
中有这个属性produces = "text/plain;charset=UTF-8"
一切工作正常在本地主机,但在实际的web服务器(Linux, Tomcat)它不工作。同样,显示utf字符可以工作,但我不能保存它们,它们被?你知道我应该去哪里找吗?
我有两个建议,你没有提到你在使用什么数据库。
1#尝试在数据库url中指定unicode和utf-8,例如:
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8
2#配置为代码
public class MvcConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
converters.add(stringHttpMessageConverter);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
希望能有所帮助