Spring MVC三(常用注解)

Spring MVC—常用注解


概述

在Spring MVC中,离不开@Component,@ Service,@ Repository和@Controller等注解,下面我们对其分别作出讲解。

1. @Component:

该注解把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>

2. @Service:

该注解用于服务层bean,示例代码:

    @Service("HelloWorldService") 
    public class HelloWorldService{...}

调用代码:

// HelloWorldService
  @Resource(name = "HelloWorldService")
  private HelloWorldService helloWorldService;

3. @Repository:

该注解用于DAO层bean(数据库访问层),示例代码:

@Repository(value="HelloWorldDao")
 public class HelloWorldDaoImpl {...}

调用代码:

@Resource(name = "HelloWorldDao")
 private HelloWorldDao helloDao;

4. @Controller:

用于控制层,表示请求映射,请求输入,异常处理等。

5. @ResponseBody:

该注解通过HttpMessageConverter将返回值序列化到body中,

6. @RequestMapping:

该注解表示处理请求的URL地址映射,它具有各种属性,可通过URL,HTTP方法,请求参数,标头进行匹配。还可以在类级别用于表示共享映射,也可以在方法级别用于缩小到特定端点映射。

@RequestMapping还拥有变体:

@GetMapping

@PostMapping

@PutMapping

@DeleteMapping

@PatchMapping

以上是开箱即用的自定义注释,因为可以说大多数控制器方法应该映射到特定的HTTP方法,使用@RequestMapping默认情况下匹配所有HTTP方法。同样 @RequestMapping,在类级别仍需要表达共享映射。如:

@RestController
@RequestMapping("/persons")
class PersonController {

    @GetMapping("/{id}")
    public Person getPerson(@PathVariable Long id) {
        // ...
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void add(@RequestBody Person person) {
        // ...
    }
}

7. @RequestBody

该注解通过HttpMessageConverter可将将request body反序列化为Object,示例:

@PostMapping("/accounts")
public void handle(@RequestBody Account account) {
    // ...
}

8. @RestController:

组合注解,相当于@Controller + @ResponseBody

9.@PathVariable

该注解表示声明URI变量,它会自动转换为适当的类型或引发TypesMismatchException,示例:

@Controller
@RequestMapping("/owners/{ownerId}")
public class OwnerController {

    @GetMapping("/pets/{petId}")
    public Pet findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
        // ...
    }
}

10.@MatrixVariable

表示矩阵变量,示例代码:

浏览器HTTP用GET请求URL: /owners/42;q=11;r=12/pets/21;q=22;s=23

@GetMapping("/owners/{ownerId}/pets/{petId}")
public void findPet(
        @MatrixVariable MultiValueMap<String, String> matrixVars,
        @MatrixVariable(pathVar="petId") MultiValueMap<String, String> petMatrixVars) {

    // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
    // petMatrixVars: ["q" : 22, "s" : 23]
}

11.@RequestParam

该注解将Servlet请求参数(即查询参数或表单数据)绑定到控制器中的方法参数中,示例代码:

@Controller
@RequestMapping("/pets")
public class EditPetController {
    @GetMapping
    public String setupForm(@RequestParam("petId") int petId, Model model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }=
}

12.@RequestHeader

该注解表示将请求标头绑定到控制器中的方法参数中,示例代码:

@GetMapping("/demo")
public void handle(@RequestHeader("Accept-Encoding") String encoding, @RequestHeader("Keep-Alive") long keepAlive) {
}

13.@CookieValue

该注解表示将HTTP cookie的值绑定到控制器中的方法参数,示例代码:

@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) {
    //...
}

14.@ModelAttribute

使用@ModelAttribute方法参数上的注释从模型中访问属性,或者如果不存在则将其实例化。model属性还覆盖了名称与字段名称匹配的HTTP Servlet请求参数的值,不必处理解析和转换单个查询参数和表单字段。示例代码:

@PostMapping("/owners/{ownerId}/pets/{petId}/edit")
public String processSubmit(@ModelAttribute Pet pet) { }

15.@SessionAttributes

该注解用于请求之间的Servlet会话中存储模型属性以供后续访问请求使用。示例代码:

@Controller
@SessionAttributes("pet")
public class EditPetForm {
    // ...
}

这段代码中,第一个请求时,如果将名为“pet”的属性添加到模型中,它将自动提升并保存在HTTP Servlet会话中,直到另一个控制器方法使用SessionStatus方法来清除它。

16.@RequestAttribute

该注解可用于访问由过滤器或拦截器创建的、预先存在的请求属性。

@GetMapping("/")
public String handle(@RequestAttribute Client client) {
    // ...
}

17.@JsonView

该注解用于序列化

18.@CrossOrigin

该注解表示能够对注解的控制器方法跨域请求,示例:

@RestController
@RequestMapping("/account")
public class AccountController {

    @CrossOrigin
    @GetMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }

    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {

    @CrossOrigin("http://domain2.com")
    @GetMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }

    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值