Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回Mono和Flux error总是返回500_Java_Exception_Mono_Spring Webflux_Flux - Fatal编程技术网

Java 返回Mono和Flux error总是返回500

Java 返回Mono和Flux error总是返回500,java,exception,mono,spring-webflux,flux,Java,Exception,Mono,Spring Webflux,Flux,我仍在努力学习WebFlux异常是如何工作的,据我所知,在Flux或Mono中返回对象时,使用者应该收到从服务器发送的相同异常 然而,当我在Mono中返回HTTPException 401时,例如,我在消费者中收到的响应正文与我发送的不同,我读取的是500内部服务器错误,而不是401 下面是我为这个问题制作的一个简单的控制器类 package com.example.demo; import javax.xml.ws.http.HTTPException; import org.spring

我仍在努力学习WebFlux异常是如何工作的,据我所知,在Flux或Mono中返回对象时,使用者应该收到从服务器发送的相同异常

然而,当我在Mono中返回HTTPException 401时,例如,我在消费者中收到的响应正文与我发送的不同,我读取的是500内部服务器错误,而不是401

下面是我为这个问题制作的一个简单的控制器类

package com.example.demo;

import javax.xml.ws.http.HTTPException;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Mono;

@RestController
public class Controller {

@RequestMapping(
        path="/getExceptionMono",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<String> getException(){
        return Mono.error(new HTTPException(401));
    }
}
这是使用者的控制台日志

Received error org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from GET http://localhost:8080/getExceptionMono
如何传递我的异常,以便消费者看到我在Mono中传递的原始异常?
希望我的问题很清楚,请提前感谢

这不是从应用程序返回4xx响应的正确方法。 应用程序中引发的任何类型的异常都将被WebClientResponseException包装,并在客户端作为500内部服务器错误接收。

更改的一种方法是在控制器中使用异常处理程序,如下所示:

  @ExceptionHandler({UnsupportedMediaTypeException.class})
  public ResponseEntity<String> exceptionHandler(Exception ex) {
    return ResponseEntity.badRequest().body(ex.getMessage());
  }
@RequestMapping(
        path="/getExceptionMono",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<ResponseEntity> getException(){
        return Mono.just(ResponseEntity.badRequest().build());
    }
}
webClient
 .get()
 .uri("/some/url")
 .exchange()
 .flatMap(clientResponse -> {
     if (clientResponse.statusCode().is5xxServerError()) {
        //do something and return some mono
     }
     else if(clientResponse.statusCode().is4xxClientError()) {
        //do something and return some mono
     }
     else {
       //do something and return some mono  
     }
});

您可以使用onStatus并检查响应代码吗?Mono result=client.get().uri(“/persons/{id}”,id).accept(MediaType.APPLICATION_JSON).retrieve().onStatus(HttpStatus::is4xxclientorror,response->…).onStatus(HttpStatus::is5xxServerError,response->…).bodytomino(Person.class);只是尝试了这个建议,它只是直接跳过4xx谓词到5xx,打印响应并返回“org.springframework.web.reactive.function.client”。DefaultClientResponse@c7a55c43“@EdenDupont请把我的答案再看一遍。我希望它能澄清您的疑问,现在事情似乎变得更清楚了,因此发送对象的正确方式也是由ResponseEntity包装的,比如说我想发送一个字符串,它在控制器中是这样的:public Mono getException(){return Mono.just(ResponseEntity.ok().body(new string(“MyString”);}是否正确?如果直接返回对象,spring将默认将响应代码设置为200。若您需要202 accepted或204 no content之类的其他内容,则需要返回响应实体
@RequestMapping(
        path="/getExceptionMono",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<ResponseEntity> getException(){
        return Mono.just(ResponseEntity.badRequest().build());
    }
}
webClient
 .get()
 .uri("/some/url")
 .exchange()
 .flatMap(clientResponse -> {
     if (clientResponse.statusCode().is5xxServerError()) {
        //do something and return some mono
     }
     else if(clientResponse.statusCode().is4xxClientError()) {
        //do something and return some mono
     }
     else {
       //do something and return some mono  
     }
});