简介:
@RequestBody
作用:
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
使用时机:
A) GET、POST方式提时, 根据request header Content-Type的值来判断:
- application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
- multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
- 其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
B) PUT方式提交时, 根据request header Content-Type的值来判断:
- application/x-www-form-urlencoded, 必须;
- multipart/form-data, 不能处理;
- 其他格式, 必须;
@ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
- @RequestMapping(value = "/getForm")
- @ResponseBody
- public String getForm(String pid) {
- return "你好";
- }
返回结果“你好”
- @RequestMapping(value = "/getForm")
- @ResponseBody
- public List<String> getForm(String pid) {
- return new ArrayList<String>(){{
- add("你好")
- }};
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <context:component-scan base-package="cn.netcluster.workflow.**" />
- <mvc:annotation-driven />
- <mvc:default-servlet-handler />
- <bean
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- id="multipartResolver">
- <property name="defaultEncoding" value="UTF-8" />
- <property name="uploadTempDir" value="resources/attach/temp" />
- </bean>
- </beans>
使用的是Spring3.1
问题补充:
xiaoZ5919 写道
你遇到的是第一个方法直接返回String会乱码,而返回list的那个不会乱码是吧?
这可以说是spring mvc的一个bug,spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的
解决的办法:
你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入
另外一种直接放弃String,而是使用对象
这可以说是spring mvc的一个bug,spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的
- public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
解决的办法:
你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <util:list>
- <bean class="com.pcds.ecomm.website.syscustomization.ConfigurableStringHttpMessageConverter">
- <constructor-arg value="UTF-8"/>
- </bean>
- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
- </util:list>
- </property>
- </bean>
另外一种直接放弃String,而是使用对象
我之前是这样写的,也没有用
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.StringHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/plain;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <bean
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
- </list>
- </property>
- </bean>
问题补充:
xiaoZ5919 写道
- if (writeAcceptCharset) {
- outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
- }
- MediaType contentType = outputMessage.getHeaders().getContentType();
- Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
- FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));
兄弟我一直都设置了这个
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
问题补充:
xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
兄弟还是不行哦
- @RequestMapping(value = "/getForm")
- @ResponseBody
- public String getForm(HttpServletResponse response, String pid) {
- response.setContentType("text/html; charset=UTF-8");
- System.out.println(super.formService.getRenderedStartForm(pid)
- .toString());
- return "你好";
- }