我正在使用简单的 @RestController 开发一个 Spring Boot 应用程序。
这是让我烦恼的代码片段。
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);
其背后的想法:当评论字段为空时,返回 400 状态(错误请求)。
我尝试了多个JEE(基于avax.ws.rs)异常,它们在中继消息等方面工作正常,但 postman 返回的状态始终为500。
不确定我错过了什么,有什么想法吗?
最佳答案
您可以尝试定义自己的异常并对其进行注释
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="comment field is empty")
查看更多here
关于java - 无论我使用哪个 JEE 异常(exception),Postman 都会返回状态 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274150/