美文网首页
org.springframework.web.bind.Mis

org.springframework.web.bind.Mis

作者: wyn_做自己 | 来源:发表于2021-12-17 11:43 被阅读0次

    今日份鸡汤:熬过了必须的苦,才能过上喜欢的生活,在一切变好之前,我们总要经历一些不开心的日子,这段日子也许很长,也许只是一觉醒来,所以耐心点,给好运一点时间。

    异常详细信息:
    org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'book' for method parameter type Book is not present

    异常复现demo:

    @RestController
    public class GetRequestParamDemo {
    
        @RequestMapping(path = "/requestParamTest")
        public Book requestParamTest(@RequestParam Book book) {
            return book;
        }
    }
    

    运行结果展示:


    image.png

    对于这种实体结构的可以使用@RequestBody来接收

        @RequestMapping(path = "/postParamTest")
        public Book postParamTest(@RequestBody Book book) {
            return book;
        }
    

    运行结果展示:


    image.png

    对于@RequestParam注解的具体用法

        @RequestMapping(path = "/requestParamTest")
        public String requestParamTest(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "id", required = true) int id) {
            return "this is " + name + " and his number is " + id;
        }
    

    运行结果展示:


    image.png

    @RequestParam注解标注的参数,如果参数不是必填参数,可以设置required = false。

    另外再描述一个异常信息:

    Resolved [org.springframework.web.bind.MissingRequestCookieException: Required cookie 'myCookie' for method parameter type String is not present]
    其实这个同理,是因为Cookie信息没有传导致的

    异常复现demo:

    @RestController
    public class GetRequestParamDemo {
    
        @RequestMapping(path = "/headerAndCookieTest")
        public String headerAndCookieTest(@RequestHeader(name = "myHeader") String myHeader, @CookieValue(name = "myCookie") String myCookie) {
            return "myHeader: " + myHeader + ",and myCookie: " + myCookie;
        }
    }
    

    运行结果展示:


    image.png image.png

    解决也很明显了,增加相应cookie参数就可以了:


    image.png

    Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'myHeader' for method parameter type String is not present]
    上面这个异常就是没有传header导致的了,不具体演示了。

    相关文章

      网友评论

          本文标题:org.springframework.web.bind.Mis

          本文链接:https://www.haomeiwen.com/subject/vrqifrtx.html