【问题标题】:Postman returns status 500 regardless of which JEE exeption do I use无论我使用哪种 JEE 异常,邮递员都会返回状态 500
【发布时间】:2017-04-07 09:08:56
【问题描述】:

我正在使用简单的@RestController 开发一个 Spring Boot 应用程序。

这是困扰我的代码 sn-p。

  else if (validators.isCommentValid(submission) == false)
  throw new WebApplicationException("comment field is empty", 400);
  //throw new BadRequestException("Comment field cannot be empty");
  //throw new HTTPException(400);

背后的思想:当comment field为空时,返回400状态(Bad Request)。

我尝试了多个 JEE(基于 avax.ws.rs)异常,它们在中继消息等方面工作正常,但邮递员返回的状态始终为 500。

不知道我错过了什么,有什么想法吗?

【问题讨论】:

    标签: java spring exception-handling jax-rs


    【解决方案1】:

    您可以尝试定义自己的异常并对其进行注释

    @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="comment field is empty")
    

    查看更多here

    【讨论】:

    • 非常感谢,这正是我所做的(我还添加了@ControllerAdvice 以使它们井井有条),它就像一个魅力。
    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 2015-08-14
    • 2013-06-29
    • 1970-01-01
    • 2021-01-04
    • 2011-11-27
    相关资源
    最近更新 更多