PostMapping,GetMapping不固定路径的写法

2019-10-23 23:25:22


PostMapping中的value属性是数组,所以可以定义多个路径,required属性默认是true,不必再写required=true,默认表示该参数是必须要有的,如果写required=false,表示该参数是可选的,可有可无。

    @PostMapping("/queryCurWeatherNullById/{id}/{name}")
    @Override
    public List<WeatherPO> queryCurWeatherNullById(@PathVariable("id") Long id,
                                                   @PathVariable("name") String name) {

如上所示,如果路径是/queryCurWeatherNullById/1/str正确,但是如果是/queryCurWeatherNullById/queryCurWeatherNullById/1则报错404 NOT FOUND.原因是id,name属性默认是必须要有的。

    @PostMapping(value = {"/queryCurWeatherNullById/{id}", "/queryCurWeatherNullById/{id}/{name}"})
    @Override
    public List<WeatherPO> queryCurWeatherNullById(@PathVariable(value = "id") Long id,
                                                   @PathVariable(value = "name", required = false) String name) {

如上所示,如果路径是http://localhost:8080/weather/queryCurWeatherNullById/1/str正确,是http://localhost:8080/weather/queryCurWeatherNullById/1正确,但是如果是http://localhost:8080/weather/queryCurWeatherNullById则报错404 NOT FOUND.原因是id属性默认是必须要有的。


所以如果某个参数可能为空,则需要定义required=false,


    @PostMapping(value = {"/queryCurWeatherNullById/{id}", "/queryCurWeatherNullById/{id}/{name}"})
    @Override
    public List<WeatherPO> queryCurWeatherNullById(@PathVariable(value = "id", required = false) int id,
                                                   @PathVariable(value = "name", required = false) String name) {


关于报错500,此时id是可选的,当路径中不含有id时,则报错如下:


java.lang.IllegalStateException: Optional int parameter 'id' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.

    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.handleNullValue(AbstractNamedValueMethodArgumentResolver.java:238)

    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:111)


原因是int类型不能为null,id的类型是int,但是Integer可以为null,把id修改为Integer类型即可。(这个错误很隐蔽!!!!)


当然如果是long,float,double原始类型也可能会报错,所以数字类的参数类型最后都是引用类型


  • 2019-12-14 21:06:58

    vue----keep-alive缓存,activated,deactivated两个生命周期函数,,meta实现缓存

    如果没有缓存,每点击一次导航,内容区就会创建一个组件,该组件会经历整个生命周期,每点击一次,就会创建一个组件,比较浪费性能, 这时,我们就要考虑到是否能将点击过的已创建的组件进行缓存,当再次点击已访问过的组件时,这时,就会从缓存中获取该组件,而不会重新创建,

  • 2019-12-17 11:56:05

    ffmpeg concat video and mix audio,ffmpeg简单快速的合并视频

    在ffmpeg中,官网给出两种连接媒体文件(音频、视频、etc..)的解决方案。 the concat "demuxer" the concat "protocol" 对比而言, demuxer更加灵活一些,需要媒体文件是属于相同的编解码器,但是可以属于不同的容器格式(mp3,wav, mp4, mov, etc..). 而protocol只适用于少数集中容器格式。

  • 2019-12-17 11:58:55

    FFmpeg文章目录

    seek ffmpeg # How to seek in mp4/mkv/ts/flv ffmpeg # flags &= ~AVSEEK_FLAG_BACKWARD ffmpeg # AVSEEK_FLAG concat ffmpeg # concat 连接两个视频 ffmpeg # -f concat -i mylist.txt ffmpeg # concat详解+音画同步策略 截图

  • 2019-12-18 23:26:00

    FFMPEG命令记录

    ffmpeg,拼接两个音频,剪切音频片段,多个音频混音,剪切一段MP4并转换成gif,改变音量大小,音频淡入淡出,音频格式处理

  • 2019-12-19 00:04:44

    ffmpeg concat video and mix audio

    在ffmpeg中,官网给出两种连接媒体文件(音频、视频、etc..)的解决方案。 the concat "demuxer" the concat "protocol" 对比而言, demuxer更加灵活一些,需要媒体文件是属于相同的编解码器,但是可以属于不同的容器格式(mp3,wav, mp4, mov, etc..). 而protocol只适用于少数集中容器格式。

  • 2019-12-19 00:16:30

    android采用FFmpeg实现音频混合与拼接剪切

    接触FFmpeg有一段时间了,它是音视频开发的开源库,几乎其他所有播放器、直播平台都基于FFmpeg进行二次开发。本篇文章来总结下采用FFmpeg进行音频处理:音频混合、音频剪切、音频拼接与音频转码。