方式2:在springMvc的核心配置文件中添加JSON乱码问题配置
乱码演示
我的接口
package com.lingaolu.controller; import com.lingaolu.bean.Student; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/student") public class StudentController { @ResponseBody @RequestMapping(value="/students") public String test(){ Student student = new Student("林某",27); return student.toString(); } }
访问
解决方式
方式1:对应的RequestMapping设置编码格式
produces = "application/json;charset=UTF-8",或者 produces =MediaType.APPLICATION_JSON_UTF8_VALUE
接口变为
package com.lingaolu.controller; import com.lingaolu.bean.Student; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/student") public class StudentController { @ResponseBody @RequestMapping(value="/students",produces = "application/json;charset=UTF-8") public String test(){ Student student = new Student("林某",27); return student.toString(); } }
测试,解决
方式2:在springMvc的核心配置文件中添加JSON乱码问题配置
<!-- JSON乱码问题配置 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="failOnEmptyBeans" value="false"/> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
测试,解决