Spring 启动 RestController,在错误状态响应主体上,错误消息为空

Spring Boot RestController, On Error Status Response Body, Message of Error is empty

在我的 Spring Boot RestController 上,我想通过抛出自定义异常将自定义错误消息传递到响应主体。我正在按照 https://dzone.com/articles/spring-rest-service-exception-handling-1

上的指南进行操作

这些是我的代码片段:

UserNotFoundException

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",

在一个阶段,我什至让它工作。好像我搞砸了一些点。

在日志中您甚至可以看到“找不到用户”文本和正确的异常 Class

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 中更改了行为。在您的应用程序中更改 属性 server.error.include-message = always。properties/yml 文件。

更多信息:Changes to the Default Error Page’s Content and Spring Boot server properties

创建一个 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
}

然后添加将发送响应的控制器建议 class。

@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);
    }

}