Spring MVC注解汇总

来源:互联网 发布:fzltchjw gb1 0 mac 编辑:程序博客网 时间:2024/05/01 21:35

@Controller
作用在类上,一般和@RequestMapping结合使用,把类做成控制器,管理http请求。

@Service
把类做成服务模块。

@RequestMapping
用于匹配http请求,作用在类上或方法上,如果作用在类上的是”/auth”,作用在方法上的是”/login”,那么完全的匹配地址就是”/auth/login”。

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
配置@RequestMapping时,可以指定method,这几个注解则是已指定method的@RequestMapping,一般情况下使用get和post方法足够了。

@PathVariable
如果在方法上的注解是:@GetMapping(“/owners/{ownerId}”),请求的URL是:/owners/13546,那么,
public String doSomething(@PathVariable String ownerId, Model model) {}
方法执行时,ownerId的值是13546.

@RequestParam
作用在方法参数上,用于获取客户端传递过来的参数。

@RequestHeader
作用在方法参数上,用于获取客户端请求的头信息。

@RequestBody
作用在方法参数上,用于获取客户端请求传递过来的内容。

@RequestPart
作用在方法参数上,用于获取表单内容。

@SessionAttribute
作用在方法参数上,用于获取通过 session.setAttribute 设置的会话对象。

@RequestAttribute
作用在方法参数上,用于获取通过 request.setAttribute 设置的request对象。

@ModelAttribute
在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里。
如果把@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法。

@ResponseBody
作用在方法参数上,方法执行完毕时,返回的对象会被解析成json返回给客户端。如果不加此注解,返回字符串时,会跳转。

@RestController
此注解和@Controller一样作用在类上,区别是此注解使用后,类内的HTTP方法相当于加上@ResponseBody。

@CookieValue
作用在方法参数上,用于获取HTTP请求里的Cookie信息。

0 0
原创粉丝点击