
spring mvc4 restful api 乱码问题
一,修改spring-mvc.xml
添加如下:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="prettyPrint" value="true" /> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> 二,修改方法,添加编码 @RequestMapping(value = "/get/{msg}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET) @ResponseBody public String get(@PathVariable("msg") String msg) { String result = "[GET] Hello " + msg; return result; }👁️ 阅读量:0
© 版权声明:本文《spring mvc4 restful api 乱码问题》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686614931a305968.html。