方式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>
测试,解决
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。