---
title: "SpringMVC注解说明"
author: "marb"
date: "2014-11-04T10:23:35Z"
tags: []
categories: ["开源软件"]
slug: "springmvc"
draft: false
lastmod: "2014-11-04T10:23:35Z"
id: 2152142
url: "https://reeboo.iteye.com/blog/2152142"
---

**@controller**

通过@controller标注即可将class定义为一个controller类。

 

**@RequestMapping**

 

**value** 表示需要匹配的url的格式。 **method** 表示所需处理请求的http 协议(如get,post,put,delete等)，可选值为RequestMethod这个enum的值。 **params** 格式为”paramname=paramvalue” 或 “paramname!=paramvalue”。 表示参数必须等于某值，或者不等于才进入此映射方法。不填写的时候表明不限制 **headers** 用来限定对应的reqeust请求的headers中必须包括的内容，例如headers={"Connection=keep-alive"}, 表示请求头中的connection的值必须为keep-alive。

 

 

 

 

 

 

@RequestParam

 

value 对应表单name空间的值 required 是否允许为空 defaultValue 默认值

 

@PathVariable

获得地址栏中传的参数 例如：

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping(value="/{groupId}.do")
2. public void detail(@PathVariable long groupId){
3. groupRepository.selectOne(groupId);
4. }

 

@RequestBody

在参数之前加入@RequestBody注解。用来将指定的客户端发送过来的请求参数的数据格式转换成java实体

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping(value = "/xxxxx.do")
2. public void create(@RequestBody() String host){
3. System.out.println("-----------" + host);
4. }

@RequestHeader

在参数之前加入@RequestHeader注解。用来将指定的请求头信息影射为方法的参数。

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping(value = "/xxxxx.do")
2. public void create(@RequestHeader() MultiValueMap&lt;String, String&gt; host){
3. System.out.println("-----------" + host);
4. }

@ResponseBody

如果这个方法定义了@ResponseBody注解。那么会把返回值转换成这个数据格式，输出给客户端

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping(value = "/xxx.do")
2. @ResponseBody
3. public MultiValueMap&lt;String, String&gt; create(@RequestHeader() MultiValueMap&lt;String, String&gt; hosts) throws Exception {
4. return hosts;
5. }

@ResponseStatus  
返回一个指定的http response状态码。

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @ResponseStatus(reason="no reason",value=HttpStatus.BAD\_REQUEST)
2. @RequestMapping("/responsestatus")
3. public void responseStatusTest(){
4. }

@SessionAttributes

写在类级别的注解，定义一个session attributes，属性名字为SessionAttributes指定。可以指定多个（数组），也同时可以指定类型。

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

01. @Controller
02. @SessionAttributes( { "user" })
03. @RequestMapping("/test")
04. public class ControllerTest {
05. @RequestMapping("/session")
06. @ResponseBody
07. public String sessionIn(@ModelAttribute("user") User user) {
08. return "index";
09. }
10. }

@CookieValue

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping("/cookie")
2. @ResponseBody
3. public String cookie(@CookieValue("JSESSIONID") String sessionId) {
4. return sessionId;
5. }

**@InitBinder** 

在controller中注册一个customer protperty editor以解析request中的参数并通过date bind机制与handler method中的参数做绑定。

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @InitBinder
2. public void initBinder(WebDataBinder binder) {
3. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
4. dateFormat.setLenient(false);
5. binder.registerCustomEditor(Date.class, new CustomDateEditor(
6. dateFormat, false));
7. }

 

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping("/databind1")
2. public ModelAndView databind1(Date date) {
3. …
4. }

访问url http://localhost:8080/springmvc/databind1.action?date=2000-01-02  
通过initbinder中注册的customeDateEditor类型，自动将2000-01-02转换为日期类型

 

**@ExceptionHandler**

**\[java]** [view plain](http://blog.csdn.net/w40338544/article/details/6881464 "view plain")[copy](http://blog.csdn.net/w40338544/article/details/6881464 "copy")

 

1. @RequestMapping("/exception")
2. public void ExceptionTest() throws Exception{
3. throw new Exception("i don't know");
4. }
5. @ExceptionHandler
6. public String handleException(Exception e,HttpServletRequest request){
7. System.out.println(e.getMessage());
8. return "helloworld";
9. }

 

 

本文部分收集于其他博文。如有不全请大家补充，如有错误请指正。