[email protected]眾匯雲聚 QQ群:437393312
阿新 • • 發佈:2019-02-10
springboot亂碼現象可以用以下方法來解決,本人是以防萬一,所以都設定了。
1.jsp檔案編碼方式設定為UTF-8,如果新建的jsp檔案,預設不是utf-8,在eclipse中,設定新建的jsp檔案為utf-8
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
2.在application.properties中新增如下
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
3.在@RequestMapping中新增produces="application/json;charset=utf-8",如下
@RequestMapping(value="hello",produces="application/json;charset=utf-8")
4.新增一個訊息轉換器
@Configuration public class CustomMVCConfiguration extends WebMvcConfigurerAdapter { @Bean public HttpMessageConverter<String> responseBodyConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8")); return converter; } public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } public void configureContentNegotiation( ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); }