1、@RequestMapping
这是一个用来处理请求地址映射的注解,可以用于类、方法上。用于类上表示整个类的父路径。
2、@RequestBody
不能处理get请求
参数是不会被Servlet转化统一放在request对象的Param参数集中
不能处理Content-Type为application/x-www-form-urlencoded编码格式的数据(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
3、@RequestParam
可以处理get、post请求
参数会被Servlet转化统一放在request对象的Param参数集中
4、@ResponseBody
用于controller层的方法上,用于:将方法返回的对象、值,通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据,此注解不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
5、@PathVariable将URL中的{xxx}占位符绑定到处理方法的入参中
例子url:/{id};入参:@PathVariable("id") String id
6、@Controller
用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法
7、@Service
用于标注业务层组件
8、@Repository
用于标注数据访问组件,即DAO组件
9、@Component
当组件不好归类的时候,我们可以使用这个注解进行标注
Spring会把带有@Controller @Service @Repository @Component四个注解的类纳入Spring的管理。使用spring注入,减少代码耦合。
10、@RequestBody和@RequestParam比较
两个注解都用于接受参数。@RequestBody不能处理Content-Type为application/x-www-form-urlencoded,@RequestBody不能处理get请求。而@RequestParam则可以。所以推荐使用@RequestParam。
11、@RequestBody和@ResponseBody
一个用于接受参数。一个用于返回参数。两个注解会调用HttpMessageConverter的read和write方法
@RequestBody调用HttpMessageConverter的read方法,调用实现类AbstractHttpMessageConverter的read方法,根据输入类型分别调用AbstractHttpMessageConverter的实现类。假设参数类型为String则调用StringHttpMessageConverter的readInternal方法。该方法会从HttpInputMessage中获取字符集,使用该字符集读取参数(HttpInputMessage中的字符集为web.xml中CharacterEncodingFilter的encoding值);
@ResponseBody调用HttpMessageConverter的write方法,AbstractHttpMessageConverter的write方法,StringHttpMessageConverter的writeInternal方法。该方法会从HttpOutputMessage中获取字符集。使用该字符集返回(HttpOutputMessage中的字符集为:StringHttpMessageConverter配置的ISO-8859-1)因此返回类型为String时会导致乱码。
12、@ResponseBody导致的乱码解决方式
- 修改spring_mvc.xml中HttpMessageConverter字符集
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:annotation-driven> 标签,自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
<mvc:message-converters> 标签,设定字符集和json处理类
- 重写StringHttpMessageConverter类:
spring_mvc.xml
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="xyz.chanjkf.utils.JsonStringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
/**
* content-type:application/json请求的乱码
* @author yi
*/
public class JsonStringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final Charset defaultCharset;
public JsonStringHttpMessageConverter() {
this(DEFAULT_CHARSET);
}
public JsonStringHttpMessageConverter(Charset defaultCharset) {
super(new MediaType[]{new MediaType("application", "json", defaultCharset), MediaType.ALL});
this.defaultCharset = defaultCharset;
}
@Override
public boolean supports(Class<?> clazz) {
return String.class.equals(clazz);
}
@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
Charset charset = this.getContentTypeCharset(inputMessage.getHeaders().getContentType());
return StreamUtils.copyToString(inputMessage.getBody(), charset);
}
@Override
protected Long getContentLength(String s, MediaType contentType) {
Charset charset = this.getContentTypeCharset(contentType);
try {
return Long.valueOf((long)s.getBytes(charset.name()).length);
} catch (UnsupportedEncodingException var5) {
throw new IllegalStateException(var5);
}
}
@Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
Charset charset = this.getContentTypeCharset(outputMessage.getHeaders().getContentType());
StreamUtils.copy(s, charset, outputMessage.getBody());
}
private Charset getContentTypeCharset(MediaType contentType) {
return contentType != null && contentType.getCharSet() != null?contentType.getCharSet():this.defaultCharset;
}
}
maven依赖:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>