spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的。
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
返回字符串 String
1.@RequestMapping(value = "/getForm")
2.@ResponseBody
3.public String getForm(String pid) {
4. return "你好";
5.}
返回List数组
1. @RequestMapping(value = "/getForm")
2.@ResponseBody
3.public List<String> getForm(String pid) {
4. return new ArrayList<String>(){{
5. add("你好")
6. }};
7.}
解决方法:
Spring 配置中加入
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<property name="customArgumentResolvers">
<bean class="MapArgumentResolver"></bean>
</property>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
/**
* 参数解析成Map<String,Object>
*/
public class MapArgumentResolver implements WebArgumentResolver {
@SuppressWarnings("unchecked")
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest();
Map<String, ?> param = req.getParameterMap();
if(param == null){
return null;
}
Map<String,Object> valueMap = new HashMap<String,Object>();
for (Iterator<String> itr = param.keySet().iterator(); itr.hasNext();) {
String name = itr.next();
String[] args =(String[])param.get(name);
if(args!=null && args.length >0){
valueMap.put(name,args[0]);
}
}
return valueMap;
}
}
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
返回字符串 String
1.@RequestMapping(value = "/getForm")
2.@ResponseBody
3.public String getForm(String pid) {
4. return "你好";
5.}
返回List数组
1. @RequestMapping(value = "/getForm")
2.@ResponseBody
3.public List<String> getForm(String pid) {
4. return new ArrayList<String>(){{
5. add("你好")
6. }};
7.}
解决方法:
Spring 配置中加入
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<property name="customArgumentResolvers">
<bean class="MapArgumentResolver"></bean>
</property>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
/**
* 参数解析成Map<String,Object>
*/
public class MapArgumentResolver implements WebArgumentResolver {
@SuppressWarnings("unchecked")
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception {
HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest();
Map<String, ?> param = req.getParameterMap();
if(param == null){
return null;
}
Map<String,Object> valueMap = new HashMap<String,Object>();
for (Iterator<String> itr = param.keySet().iterator(); itr.hasNext();) {
String name = itr.next();
String[] args =(String[])param.get(name);
if(args!=null && args.length >0){
valueMap.put(name,args[0]);
}
}
return valueMap;
}
}