whitelabel错误页,而不是缺少必需的参数

mbzjlibv  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(354)

我有一个 GET /object 使用所需参数调用 id :

@RequestMapping(value = {"/object"}, produces = "application/json", method = RequestMethod.GET)
public String getObject(@RequestParam(required = true) String id, HttpServletRequest request) {

    // do stuff
    ...

    // Send message
    return json;
}

当它在没有参数的情况下被调用时 id 我的spring应用程序抛出一个: Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'id' is not present] 但打电话的人收到 Whitelabel Error Page 用一个 400 状态,没有关于缺少参数的解释。。。
有没有办法把缺少的参数返回给调用者?

ttp71kqs

ttp71kqs1#

您需要创建一个自定义错误页,它将描述您声明的异常。请看这里:https://www.baeldung.com/exception-handling-for-rest-with-spring

相关问题