@RequestMapping和@GetMapping @PostMapping的区别、及@PathVaribale/@RequestParam介绍

@RequestMapping和@GetMapping @PostMapping的区别

@GetMapping:将HTTP get请求映射到特定处理程序的方法注解,是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写

  1. 源码如下:
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @RequestMapping(
        method = {RequestMethod.GET}
    )
    public @interface GetMapping {
        @AliasFor(
            annotation = RequestMapping.class
        )
        String name() default "";
        ....
    }
    
  2. @RequestMapping(method = RequestMethod.GET)这行代码即说明@GetMapping就是@RequestMapping附加了请求方法

@PostMapping:将HTTP post请求映射到特定处理程序的方法注解,是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写

  1. 源码如下:
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @RequestMapping(
        method = {RequestMethod.POST}
    )
    public @interface PostMapping {
        @AliasFor(
            annotation = RequestMapping.class
        )
        String name() default "";
        .......
    }
    
  2. 如上类似

总结:除了上面二者的引入,还有@PostMapping、@PutMapping、@DeleteMapping和@PatchMapping,一共5个注解,所以,一般情况下用下面的注解也可

@RequestMapping(method = RequestMethod.XXXX)

@PathVaribale/@RequestParam介绍

@PathVaribale:获取url中的数据

需要获取localhost:8080/abc/id中的id值,实现代码如下

  • 前提:通过@PathVariable注解来获取URL中的时参数的前提条件是我们知道url的格式时怎么样的。只有知道url的格式,我们才能在指定的方法上通过相同的格式获取相应位置的参数值
@RestController
public class AbcController {

    @RequestMapping(value="/abc/{id}",method= RequestMethod.GET)
    public String test(@PathVariable("id") Integer id){
        return "id:"+id;
    }
}

在url有多个参数需要获取

@RestController
public class AbcController {

    @RequestMapping(value="/abc/{id}/{name}",method= RequestMethod.GET)
    public String test(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return "id:"+id+" name:"+name;
    }
}

@RequestParam:获取请求参数的值

url的格式为:localhost:8080/abc?id=98,获取id值,get请求和post请求都是这样

@RestController
public class AbcController {

    @RequestMapping(value="/abc",method= RequestMethod.GET)
    //或
    //@GetMapping("/abc")
    public String test(@RequestParam("id") Integer id){
        return "id:"+id;
    }
}

url格式:localhost:8080/abc?id=98&&name=abc1,获取id和name值,get请求和post请求都是这样

@RestController
public class AbcController {

    @RequestMapping(value="/abc",method= RequestMethod.GET)
    //或
    //@GetMapping("/abc")
    public String test(@RequestParam("id") Integer id,@RequestParam("name") String name){
        return "id:"+id+ " name:"+name;
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

?abc!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值