admin管理员组

文章数量:1794759

spring mvc4 restful api 乱码问题

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; }

本文标签: 乱码springAPIrestful