Spring boot 使用spring boot webflux处理错误消息

Spring boot 使用spring boot webflux处理错误消息,spring-boot,spring-webflux,project-reactor,Spring Boot,Spring Webflux,Project Reactor,我正在编写一个java适配器,用于将内容发布到Web服务。 在http服务中,我使用ExceptionHandler: @org.springframework.web.bind.annotation.ExceptionHandler public ResponseEntity<String> handle(BadCommandException ex) { return ResponseEntity.badRequest().body(ex.getMessage()); }

我正在编写一个java适配器,用于将内容发布到Web服务。 在http服务中,我使用ExceptionHandler:

@org.springframework.web.bind.annotation.ExceptionHandler 
public ResponseEntity<String> handle(BadCommandException ex) {
  return ResponseEntity.badRequest().body(ex.getMessage());
}
@org.springframework.web.bind.annotation.ExceptionHandler
公共响应属性句柄(BadCommandException ex){
返回ResponseEntity.badRequest().body(例如getMessage());
}
如果我把它称为curl/postman,效果会很好

但是,如何才能通过反应式网络客户端获取正文中的错误消息?
我的客户端库的一部分:

private Mono<Optional<String>> post(String bearerToken, Model model, IRI outbox) {
    return WebClient.builder()
        .build()
        .post()
        .uri(outbox.stringValue())
        .contentType(new MediaType("text","turtle"))
        .body(BodyInserters.fromValue(modelToTurtleString(model)))              
        .header("Authorization", "Bearer " + bearerToken)
        .header("profile", "https://www.w3.org/ns/activitystreams")
        .accept(new MediaType("text", "turtle"))
        .retrieve()         
        .toEntity(String.class)
        .map(res->res.getHeaders().get("Location").stream().findFirst());
}
private Mono post(字符串bearerToken、模型模型、IRI发件箱){
返回WebClient.builder()
.build()
.post()
.uri(发件箱.stringValue())
.contentType(新媒体类型(“文本”、“海龟”))
.body(BodyInserters.fromValue(modelToTurtleString(model)))
.标题(“授权”、“持票人”+持票人)
.header(“profile”https://www.w3.org/ns/activitystreams")
.accept(新媒体类型(“文本”、“海龟”))
.retrieve()
.toEntity(String.class)
.map(res->res.getHeaders().get(“位置”).stream().findFirst());
}
在成功的情况下,我想从location头返回字符串。但是,例如,如果http状态为400,我如何抛出异常,并将主体中的错误消息添加到异常中

谢谢

spring提供了如何处理错误的示例。我建议你从那里开始

Mono<Person> result = client.get()
        .uri("/persons/{id}", id).accept(MediaType.APPLICATION_JSON)
        .retrieve()
        .onStatus(HttpStatus::is4xxClientError, response -> ...)
        .onStatus(HttpStatus::is5xxServerError, response -> ...)
        .bodyToMono(Person.class);
Mono result=client.get()
.uri(“/persons/{id}”,id).accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatus::is4xxClientError,响应->…)
.onStatus(HttpStatus::is5xxServerError,响应->…)
.bodyToMono(个人、班级);
Javadoc:

/**
 * Create a {@link WebClientResponseException} that contains the response
 * status, headers, body, and the originating request.
 * @return a {@code Mono} with the created exception
 * @since 5.2
 */
Mono<WebClientResponseException> createException();
/**
*创建包含响应的{@link WebClientResponseException}
*状态、标头、正文和原始请求。
*@返回一个{@code Mono}和创建的异常
*@自5.2
*/
Mono-createException();
我的问题是我的测试用例“测试错误请求”。由于重构,它发送了一个空的请求体而不是一个坏的请求体,并且注释@RequestBody有一个默认为true的属性“required”。因此,响应正文始终为空,并且未按预期包含我的错误消息;-)

public Mono-doIt(主体,@PathVariable-String-actorId,@RequestBody-String-model){
...

这花费了我几个小时;-)

我的问题是让响应主体进入异常,我没有找到解决方案。但正如预期的那样,这仍然是一个400个错误请求;-)
/**
 * Create a {@link WebClientResponseException} that contains the response
 * status, headers, body, and the originating request.
 * @return a {@code Mono} with the created exception
 * @since 5.2
 */
Mono<WebClientResponseException> createException();
public Mono<ResponseEntity<Void>> doIt(Principal principal, @PathVariable String actorId, @RequestBody String model) {
...