Springboot中Getmapping使用PathVariable、HttpServletRequest、RequestParam获取参数

@PathVaribale 获取url中的数据

@RequestParam 获取请求参数的值


    @GetMapping(value = "/getVideoPlayInfos")
    public List<GetPlayInfoResponse.PlayInfo> getVideoPlayInfos(@RequestParam(value = "videoId") String videoId) throws Exception {
        return getVideoPlayInfoResponseList(videoId);
    }

@RequestParam注解使用后

@PathVariable使用方法

    @GetMapping(value = "/getVideoPlayInfos/{videoId}")
    public List<GetPlayInfoResponse.PlayInfo> getVideoPlayInfos(@PathVariable(value = "videoId") String videoId) throws Exception {
        return getVideoPlayInfoResponseList(videoId);
    }

 使用后,直接在url后面拼接参数

@RequestMapping是一个无方法的注解。@GetMapping和@PostMapping是组合注解,分别是@RequestMapping(method = RequestMethod.GET)和@RequestMapping(method = RequestMethod.POST)的缩写。
GET、POST是方法的映射,表示为
@RequestMapping(method = RequestMethod.${方法})

在一开始的映射是使用@RequestMapping(method = RequestMethod.${方法})来表示。后来Spring4.3中引进了@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping注解来帮助简化常用的HTTP方法的映射。

什么时候用GET,什么时候用POST?

GET方法:客户端与服务端之间是读取操作(如搜索、读、查询操作)。
POST方法:
1.交互是一个命令或订单(order),比提问包含更多信息
2.交互改变了服务器端的资源并被用户察觉,例如订阅某项服务
3.用户需要对交互产生的结果负责

根据HTTP协议规定,GET方法可以携带交互需要的所有数据,因此你会看到搜索百度或谷歌的时候,点击搜索形成的URL包含了你刚才的搜索关键字,没有安全需求的请求把信息放URL里没关系,但是你访问银行网站的时候,不希望把账户、密码这些放在URL里被人拦截是吧,所以HTTP设计了POST请求,他可以把请求信息放在HTTP请求里,具体格式这里不细说了,这样你就不能简单的从URL里找到账户、密码了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐