在我的 Spring Boot RestController 上,我想通过抛出自定义异常来将自定义错误消息传递到响应正文。我正在遵循 https://dzone.com/articles/spring-rest-service-exception-handling-1
上的指南这些是我的代码片段:
用户未发现异常
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
简化的 RestController
@RestController
public class CreateRfqController {
@PostMapping("api/create-rfq")
public ResponseEntity<Rfq> newRfq(@RequestBody Rfq newRfq) {
throw new UserNotFoundException("User Not Found");
}
}
调用我的 Rest API 时,我总是收到一条空消息:
{
"timestamp": "2020-06-24T18:23:30.846+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/api/create-rfq"
}
相反我想要
"message": "User Not Found",
在某个阶段我什至让它发挥作用。好像我在某些方面搞砸了。
在日志中,您甚至可以看到“未找到用户”文本和正确的异常类
24 Jun 2020;20:50:52.998 DEBUG = 145: o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolved [configRestServer.exceptions.UserNotFoundException: User Not Found]
24 Jun 2020;20:50:52.998 DEBUG = 1131: o.s.w.s.DispatcherServlet - Completed 404 NOT_FOUND
24 Jun 2020;20:50:53.003 DEBUG = 91: o.s.w.s.DispatcherServlet - "ERROR" dispatch for POST "/error", parameters={}
24 Jun 2020;20:50:53.006 DEBUG = 414: o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
24 Jun 2020;20:50:53.018 DEBUG = 265: o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
24 Jun 2020;20:50:53.018 DEBUG = 91: o.s.w.s.m.m.a.HttpEntityMethodProcessor - Writing [{timestamp=Wed Jun 24 20:50:53 CEST 2020, status=404, error=Not Found, message=, path=/api/create-rf (truncated)...]
24 Jun 2020;20:50:53.038 DEBUG = 1127: o.s.w.s.DispatcherServlet - Exiting from "ERROR" dispatch, status 404
我的build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.3.0.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security
compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.3.1.RELEASE'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.3'
compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12'
更新: 我故意将一些错误的变量类型传递到我的 RFQ 对象中以产生默认错误。 即使在回复中,消息也是空的:
{
"timestamp": "2020-06-24T19:20:18.831+00:00",
"status": 403,
"error": "Forbidden",
"message": "",
"path": "/api/create-rfq"
}
Spring Boot 2.3.0 中的行为已更改。更改 application.properties/yml 文件中的属性 server.error.include-message = always。
创建一个 POJO 来表示您的响应
"message": "User Not Found"
public class ExceptionMessage {
String message;
String details;
public ExceptionMessage(String message, String details) {
this.message = message;
this.details = details;
}
//Add getters and setters
}
然后添加一个将发送响应的控制器建议类。
@ControllerAdvice
public class CustomException {
@ExceptionHandler(UserNotFoundException.class)
public final ResponseEntity<ExceptionMessage> UserNotFoundException(UserNotFoundException ex, WebRequest request) {
ExceptionMessage error = new ExceptionMessage("Not found", ex.getMessage());
return new ResponseEntity<ExceptionMessage>(error, HttpStatus.BAD_REQUEST);
}
}