码迷,mamicode.com
首页 > 移动开发 > 详细

@RequestMapping注解

时间:2018-06-29 01:06:47      阅读:1930      评论:0      收藏:0      [点我收藏+]

标签:eth   ram   pat   ati   isp   map   col   []   content   

1、@RequestMapping可以注解在类上和方法上,当注解在类伤的时候,所有的方法都将映射为相对于类级别的请求

如下代码

  

技术分享图片
1 @RequestMapping(value="/user")
2 public class UserController{
3     // 该方法映射的请求为http://localhost:8080/user/login
4      @RequestMapping("/login")
5      public String login() {
6          return "login";
7      }
8 }
View Code

2、@RequestMapping注解说明

属性 类型  是否必要  说明 
 value String[]  否  用于将制定请求的实际地址映射到方法上 
 name String  否  给映射地址制定一个别名 
 method RequestMethod[]  否  映射指定请求的方法的类型,包括GET,POST,HEAD,OPTIONS,PUT,PATCH,DELETE,TRACE 
 consumes String[]  否  指定处理请求提交的内容类型(Content-Type),例如application/json,text/html等 
 params String[]  否  指定request中必须包含某些参数值时,才让该方法进行处理 
produces String[] 指定返回的内容类型,返回的内容类型必须是request请求头(Accept)中包含的类型
headers String[] 指定request中必须包含某些指定的header值时,才让该方法进行处理 

 

  • value属性

  

技术分享图片
1 @RequestMapping(value="/register")
2      public String registerForm() {
3         
4          return "registerForm";
5      }
View Code

 

URL访问方式如下:
http://localhost:8080/user/register
  • method属性
 @RequestMapping(value="/register",method=RequestMethod.POST)
 同时也支持多个http的请求方式
 @RequestMapping(value="/register",method={RequestMethod.POST,RequestMethod.GET})
  • consumes属性
 @RequestMapping(value="/register",method=RequestMethod.POST,consumes="application/json")
该方法标识仅畜类 request Content-Type为“application/json”类型的请求
  • produces属性
@RequestMapping(value="/register",method=RequestMethod.POST,produces="application/json")
该方法标识仅处理 request 请求头Accept头中包含了“application/json”类型的请求,同时也指明了返回的内容的类型为application/json

 











@RequestMapping注解

标签:eth   ram   pat   ati   isp   map   col   []   content   

原文地址:https://www.cnblogs.com/haizhilangzi/p/9240900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!