Spring boot 如何在SpringWebFlux中处理异常并返回正确的HTTP代码?

Spring boot 如何在SpringWebFlux中处理异常并返回正确的HTTP代码?,spring-boot,exception-handling,reactive-programming,spring-webflux,project-reactor,Spring Boot,Exception Handling,Reactive Programming,Spring Webflux,Project Reactor,我有一个方法,它使用Mono.fromfuture(result)给出响应,并抛出状态为400的CustomException。 现在在我的服务类中,当我调用该方法时,我抛出的错误和代码不会传播到我的客户机(postman)。只有信息是我所看到的 我得到以下格式:- { "timestamp": "2019-02-01T11:13:32.748+0000", "path": "/some_url", "status": 500, "error": "Interna

我有一个方法,它使用Mono.fromfuture(result)给出响应,并抛出状态为400的CustomException。 现在在我的服务类中,当我调用该方法时,我抛出的错误和代码不会传播到我的客户机(postman)。只有信息是我所看到的

我得到以下格式:-

{
    "timestamp": "2019-02-01T11:13:32.748+0000",
    "path": "/some_url",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Unable to fetch Response"
}
期望(我想要达到的目标):-

我的代码:-

public Mono<ResponseObject> fetchResponse(arg) {

   return Mono.just(somedata which will give exception)
      .flatMap(k -> callExternalService(k))
      .onErrorMap(e -> Mono.error(
            new BusinessException("Unable to fetch Response", e))

       */* e here is giving :-
        "code" : 400,
        "message" : "Request contains an invalid argument.",
        "status" : "INVALID_ARGUMENT" */*
}
public响应(arg){
返回Mono.just(将产生异常的somedata)
.flatMap(k->callExternalService(k))
.onErrorMap(e->Mono.error(
新业务异常(“无法获取响应”,e))
*/*e这里是给予:-
“代码”:400,
“消息”:“请求包含无效参数。”,
“状态”:“无效的_参数”*/*
}

你看过课程的文档了吗

您可以使用文档中的静态方法创建自己的
响应
,并在
onErrorMap
中发送它,而不是
Mono.error

您必须返回如下内容:

ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(klass, Klass.class);

ServerResponse.status(status).build();//read doc and set for yourself

你也可以检查这个。

你看过这个课程的文档了吗

您可以使用文档中的静态方法创建自己的
响应
,并在
onErrorMap
中发送它,而不是
Mono.error

您必须返回如下内容:

ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(klass, Klass.class);

ServerResponse.status(status).build();//read doc and set for yourself

您也可以对此进行检查。

Response不是预定义类。它是我自己的对象(现在编辑为ResponseObject)。您可以建议其他方法吗?路由器需要一个ServerResponse。传递ServerResponse或Response。检查文档。Response不是预定义类。它是我自己的对象(现在编辑为ResponseObject).你能建议其他方法吗?路由器需要一个ServerResponse。传递ServerResponse或Response。检查文档。