spring mvc 大部分注解

本文详细介绍了Spring MVC框架中常用的注解@Controller、@RequestMapping、@PathVariable、@RequestParam、@RequestHeader、@RequestBody和@ResponseBody的功能及用法,以及如何通过这些注解处理HTTP请求、路径变量、URL参数、HTTP头、请求体和HTTP响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@Controller
使用 @Controller 注释对将成为 MVC 中控制器的类进行注释并处理 HTTP 请求。
@RequestMapping
使用 @RequestMapping 注释对函数进行注释,该函数处理某些 HTTP 方法、URI 或 HTTP 头。此注释是 Spring REST 支持的关键。可以更改 method 参数以处理其他 HTTP 方法。

例如:

@RequestMapping(method=RequestMethod.GET, value="/emps",

headers="Accept=application/xml, application/json")

headers 限定 只有请求头中 Accept 有application/xml, application/json这样匹配的字符串才受理

以下是一次请求的报文头 headers 类容

    1. Accept:
      text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    2. Accept-Charset:
      GBK,utf-8;q=0.7,*;q=0.3
    3. Accept-Encoding:
      gzip,deflate,sdch
    4. Accept-Language:
      zh-CN,zh;q=0.8
    5. Cache-Control:
      max-age=0
    6. Connection:
      keep-alive
    7. Host:
      127.0.0.1:8080
    8. User-Agent:
      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22

security-3.1新增的参数

2: @RequestMapping 新增参数Consumes 和Produces
前面介绍过@RequestMapping的参数中有一个header的参数,来指定handler method能接受的http request 请求的header内容。
而consumes和produces则更进一步,直接指定所能接受或产生的request请求的content type。
例如

Java代码
  1. @RequestMapping(value="/testMsgConverter",consumes="text/plain",produces="application/json")

表示handlermethod接受的请求的header中的 Content-Type为text/plain;
Accept为application/json

4: Validation For @RequestBody

@RequestBody现在直接支持@valid标注了,如果validation失败,将抛出
RequestBodyNotValidException。
具体处理逻辑可见 spring 中的RequestResponseBodyMethodProcessor中的以下代码。

Java代码
  1. publicObjectresolveArgument(MethodParameterparameter,
  2. ModelAndViewContainermavContainer,
  3. NativeWebRequestwebRequest,
  4. WebDataBinderFactorybinderFactory)throwsException{
  5. Objectarg=readWithMessageConverters(webRequest,parameter,parameter.getParameterType());
  6. if(shouldValidate(parameter,arg)){
  7. StringargName=Conventions.getVariableNameForParameter(parameter);
  8. WebDataBinderbinder=binderFactory.createBinder(webRequest,arg,argName);
  9. binder.validate();
  10. Errorserrors=binder.getBindingResult();
  11. if(errors.hasErrors()){
  12. thrownewRequestBodyNotValidException(errors);
  13. }
  14. }
  15. returnarg;
  16. }


@PathVariable

使用 @PathVariable 注释可将 URI 中的路径变量作为参数插入。

例如:

@RequestMapping(method=RequestMethod.GET, value="/emp/{id}")
public ModelAndView getEmployee(@PathVariable String id) { … }



其他有用的注释
使用 @RequestParam 将 URL 参数插入方法中。

使用 @RequestHeader 将某一 HTTP 头插入方法中。

使用 @RequestBody 将 HTTP 请求正文插入方法中。

使用 @ResponseBody 将内容或对象作为 HTTP 响应正文返回。

使用 HttpEntity<T> 将它自动插入方法中,如果将它作为参数提供。

使用 ResponseEntity<T> 返回具有自定义状态或头的 HTTP 响应。

例如:

public @ResponseBody Employee getEmployeeBy(@RequestParam("name")
String name, @RequestHeader("Accept") String accept, @RequestBody String body) {…}
public ResponseEntity<String> method(HttpEntity<String> entity) {…}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值