0
点赞
收藏
分享

微信扫一扫

【刨根问底】在Springboot中MVC的常用注解<中>


接着上一篇


@RequestMapping


@RequestMapping注解是我们在开发web项目中使用最多的注解之一,前段时间面试很多人的时候,发现很多人也就是用用而已, 更奇葩的是遇到两个人干了将近三年了,说这个注解没见过或者不知道怎么用。个人认为如果你是个三年以下的程序员这主要是能干活为主,如果最基本的一些东西你都不知道或者不清楚,那么会很尴尬的。扯远了!!!回到咱们的话题,咱们来看看@RequestMapping这个注解到底是个什么东东,

talk is cheap,show me the code。这里借用网上的一张图:

【刨根问底】在Springboot中MVC的常用注解<中>_User


案例实现方式




url:http://localhost:9010/user/add

参数:userName



@RequestMapping的三种实现方式:

第一种:


@Controller	
@RequestMapping("/user")	
public class UserQueryOperateController {	
    @RequestMapping(value = "/add", method = RequestMethod.POST)	
    @ResponseBody	
    public Object addUser(String userName) {	
        //将信息存储然后返回	
        User user = new User(10001, userName);	
        return user;	
    }	
}


 第二种:


@Controller 	
public class UserQueryOperateController {	
    @RequestMapping(value = "/user/add", method = RequestMethod.POST)	
    @ResponseBody	
    public Object addUser(String userName) {	
        //将信息存储然后返回	
        User user = new User(10001, userName);	
        return user;	
    }	
}


第三种:


@RestController	
public class UserQueryOperateController {	
    @PostMapping("/user/add")	
    public Object addUser(String userName) {	
        //将信息存储然后返回	
        User user = new User(10001, userName);	
        return user;	
    }	
}


以上三分代码实现同一个功能,也是@RequestMapping使用最简单的方式。


部分源码


Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。


@Target({ElementType.TYPE, ElementType.METHOD})	
@Retention(RetentionPolicy.RUNTIME)	
@Documented	
@Mapping	
public @interface RequestMapping {	
 String name() default "";	
 @AliasFor("path")	
 String[] value() default {};	
 @AliasFor("value")	
 String[] path() default {};	
 RequestMethod[] method() default {};	
 String[] params() default {};	
 String[] headers() default {};	
 String[] consumes() default {};	
 String[] produces() default {};	
}


其中上面的Http请求方法对应着RequestMethod枚举类。


public enum RequestMethod {	
  GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE	
}


GET、POST请求方法案例:


@Controller	
public class UserQueryOperateController {	
    @RequestMapping(value = "/user/add", 	
    method = RequestMethod.POST)	
    @ResponseBody	
    public Object addUser(String userName) {	
        //将信息存储然后返回	
        User user = new User(10001, userName);	
        return user;	
    }	
    @RequestMapping(value = "/getUserById",	
    method = RequestMethod.GET)	
    @ResponseBody	
    public User getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}


匹配多URI


案例:


@Controller	
public class UserQueryOperateController {	
    @RequestMapping(value = {"/getUserById", "/getUser", "/getUserInfo/*"}, method = RequestMethod.GET)	
    @ResponseBody	
    public User getUserById(Integer id) {	
        return new User(id, "Java后端技术栈", new Date());	
    }	
}


访问方式:


http://localhost:9010/getUserById  

http://localhost:9010/getUser  

http://localhost:9010/getUserInfo/999


具有通配符匹配的URI的功能。


动态URI


案例:


@Controller	
public class UserQueryOperateController {	
    @RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)	
    @ResponseBody	
    public User getUserById(@PathVariable Integer id) {	
        return new User(id, "Java后端技术栈", new Date());	
    }	
}


访问:http://localhost:9010/getUser/{id}

【刨根问底】在Springboot中MVC的常用注解<中>_User_02

【刨根问底】在Springboot中MVC的常用注解<中>_技术栈_03


默认请求方式


案例:


@Controller	
public class UserQueryOperateController {	
    @RequestMapping(value = "/getUserById")	
    @ResponseBody	
    public User getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}


即就是没有指定请求方法的情况下,



GET http://localhost:9010/getUserById

POST http://localhost:9010/getUserById



两种方式都是可以成功的。


处理生产和消费对象 


可以使用 @RequestMapping 注解的 produces 和 consumes 这两个元素来缩小请求映射类型的范围。 为了能用请求的媒体类型来产生对象, 你要用到 @RequestMapping 的 produces 元素再结合着 @ResponseBody 注解。 你也可以利用 @RequestMapping 的 comsumes 元素再结合着 @RequestBody 注解用请求的媒体类型来消费对象。 

案例:


@Controller	
public class UserQueryOperateController {	
    @RequestMapping(value = "/produces/demo", produces = {"application/JSON"})	
    @ResponseBody	
    String getProduces() {	
        return "Produces attribute";	
    }	
    @RequestMapping(value = "/consumes/demo", consumes = {"application/JSON", "application/XML"})	
    String getConsumes() {	
        return "Consumes attribute";	
    }	
}


在这段代码中,getProduces() 处理方法会产生一个json响应, getConsumes() 处理方法可以同时处理请求中的 json和 xml内容。 


新版本模式


spring 4.3版本开始,可以使用新版本的@RequestMapping,具体如下:


@GetMapping	
@PostMapping	
@PutMapping	
@DeleteMapping	
@PatchMapping


案例:(这种使用方式有的人也称之为快捷方式)


@RestController	
public class UserQueryOperateController {	
    @PostMapping(value = "/user/add")	
    public Object addUser(String userName) {	
        //将信息存储然后返回	
        User user = new User(10001, userName);	
        return user;	
    }	
}


@PostMapping源码如下:


@Target(ElementType.METHOD)	
@Retention(RetentionPolicy.RUNTIME)	
@Documented	
@RequestMapping(method = RequestMethod.POST)	
public @interface PostMapping {	
  /**	
   * Alias for {@link RequestMapping#name}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String name() default "";	
  /**	
   * Alias for {@link RequestMapping#value}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] value() default {};	
  /**	
   * Alias for {@link RequestMapping#path}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] path() default {};	
  /**	
   * Alias for {@link RequestMapping#params}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] params() default {};	
  /**	
   * Alias for {@link RequestMapping#headers}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] headers() default {};	
  /**	
   * Alias for {@link RequestMapping#consumes}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] consumes() default {};	
  /**	
   * Alias for {@link RequestMapping#produces}.	
   */	
  @AliasFor(annotation = RequestMapping.class)	
  String[] produces() default {};	
}


另外几个类似,很容易知晓以下关系


@GetMapping = @RequestMapping + RequestMethod.GET	
@PostMapping = @RequestMapping + RequestMethod.post	
@PutMapping = @RequestMapping + RequestMethod.PUT	
@DeleteMapping = @RequestMapping + RequestMethod.DELETE	
@PatchMapping = @RequestMapping + RequestMethod.PATCH


ok,今天就到此

举报

相关推荐

0 条评论