【发布时间】:2020-06-29 18:53:59
【问题描述】:
我想将我的异常从“数据库”REST API 重新抛出到“后端”REST API,但我丢失了原始异常的消息。
这是我通过 Postman 从我的“数据库”REST API 中得到的:
{
"timestamp": "2020-03-18T15:19:14.273+0000",
"status": 400,
"error": "Bad Request",
"message": "I'm DatabaseException (0)",
"path": "/database/api/vehicle/test/0"
}
这部分没问题。
这是我通过 Postman 从我的“后端”REST API 中得到的:
{
"timestamp": "2020-03-18T15:22:12.801+0000",
"status": 400,
"error": "Bad Request",
"message": "400 BAD_REQUEST \"\"; nested exception is org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from GET http://localhost:8090/database/api/vehicle/test/0",
"path": "/backend/api/vehicle/test/0"
}
如您所见,原来的“消息”字段丢失了。
我用:
- Spring Boot 2.2.5.RELEASE
- spring-boot-starter-web
- spring-boot-starter-webflux
后端和数据库以 Tomcat (web and webflux in the same application) 开头。
这是后端:
@GetMapping(path = "/test/{id}")
public Mono<Integer> test(@PathVariable String id) {
return vehicleService.test(id);
}
使用vehicleService.test:
public Mono<Integer> test(String id) {
return WebClient
.create("http://localhost:8090/database/api")
.get()
.uri("/vehicle/test/{id}", id)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Integer.class);
}
这是数据库:
@GetMapping(path = "/test/{id}")
public Mono<Integer> test(@PathVariable String id) throws Exception {
if (id.equals("0")) {
throw new DatabaseException("I'm DatabaseException (0)");
}
return Mono.just(Integer.valueOf(2));
}
我也试过return Mono.error(new DatabaseException("I'm DatabaseException (0)"));
还有数据库异常:
public class DatabaseException extends ResponseStatusException {
private static final long serialVersionUID = 1L;
public DatabaseException(String message) {
super(HttpStatus.BAD_REQUEST, message);
}
}
似乎我的后端转换了响应,在互联网上找不到任何答案。
【问题讨论】:
-
尝试返回
Mono.error(new RestStatusException(HttpStatus.BAD_REQUEST, "I'm DatabaseException (0)")); -
我已经尝试过了,但它不起作用。
标签: java spring-boot spring-webflux spring-webclient