Han*_*oth 13 service exception spring-mvc spring-restcontroller
在我的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
简化的 RestController
@RestController
public class CreateRfqController {
@PostMapping("api/create-rfq")
public ResponseEntity<Rfq> newRfq(@RequestBody Rfq newRfq) {
throw new UserNotFoundException("User Not Found");
}
}
Run Code Online (Sandbox Code Playgroud)
调用我的 Rest API 时,我总是收到一条空消息:
{
"timestamp": "2020-06-24T18:23:30.846+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/api/create-rfq"
}
Run Code Online (Sandbox Code Playgroud)
相反,我想要 "message": "User Not Found",
在一个阶段,我什至让它工作。好像我在某些点上搞砸了。
在日志中,您甚至可以看到 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
Run Code Online (Sandbox Code Playgroud)
我的 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'
Run Code Online (Sandbox Code Playgroud)
更新:我故意将一些错误的变量类型传递到我的 RFQ 对象中以产生默认错误。即使在响应中,消息也是空的:
{
"timestamp": "2020-06-24T19:20:18.831+00:00",
"status": 403,
"error": "Forbidden",
"message": "",
"path": "/api/create-rfq"
}
Run Code Online (Sandbox Code Playgroud)
小智 35
它在 Spring Boot 2.3.0 中改变了行为。在 application.properties/yml 文件中更改属性server.error.include-message = always。
更多信息:更改默认错误页面的内容和Spring Boot 服务器属性