Java 回退工厂无法处理外部客户端中的自定义异常
我的要求是访问第一个服务抛出的自定义异常以及第二个服务中的主体内容 到目前为止,我已经尝试了两种方法,FallbackFactory和ErrorDecoder,其中只有Fallback factory对我有效。错误解码器没有从其他服务引发的异常消息。下面是我在另一个问题中发现的示例代码: 将有两种服务:库存服务和产品服务 库存服务 InventoryController.java ProductControllerAdvice.java 但是,我的问题是,当我对正在开发的应用程序尝试类似的方法时,我没有得到异常信息,相反,我得到了如下输出:Java 回退工厂无法处理外部客户端中的自定义异常,java,spring,spring-boot,feign,Java,Spring,Spring Boot,Feign,我的要求是访问第一个服务抛出的自定义异常以及第二个服务中的主体内容 到目前为止,我已经尝试了两种方法,FallbackFactory和ErrorDecoder,其中只有Fallback factory对我有效。错误解码器没有从其他服务引发的异常消息。下面是我在另一个问题中发现的示例代码: 将有两种服务:库存服务和产品服务 库存服务 InventoryController.java ProductControllerAdvice.java 但是,我的问题是,当我对正在开发的应用程序尝试类似的方法时
reached fallback on workflow side, reason: status 400 reading ProvisioningServiceProxy#executeOrderAction(Long,Long,String)
服务-A
TestServiceA.java
如果我的终端缺少任何东西,请告诉我,我已经完成了,并且在我看来,我已经按照从其他服务抛出的消息和异常体的方式完成了实现。您能够解决这个问题吗?如果是,请说明您是如何解决的?我也遇到了类似的问题。@VishalPatel从那以后已经有一段时间了,我只记得实现了错误解码器,并且我能够在响应体中获得异常消息
@FeignClient(name = "product-service", url = "http://localhost:9100", fallbackFactory = ProductServiceClientFallback.class)
public interface ProductServiceClient {
@GetMapping("/products")
ResponseEntity<?> hello();
}
@Component
class ProductServiceClientFallback implements FallbackFactory<ProductServiceClient> {
@Override
public ProductServiceClient create(Throwable cause) {
return new ProductServiceClient() {
@Override
public ResponseEntity<?> hello() {
System.out.println("hello!! fallback reason was " + cause.getMessage());
return ResponseEntity.ok().build();
}
};
}
}
@RestController
@RequestMapping(value = "/products")
public class ProductController {
@GetMapping
public String hello() throws Exception {
if (true) {
throw new Exception("Service B Exception...");
}
return "Hello World";
}
}
@RestControllerAdvice
public class ProductControllerAdvice {
@ExceptionHandler
public ResponseEntity<?> handleException(Exception exception) {
return new ResponseEntity<>("Caused due to : " + exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
hello!! fallback reason was status 500 reading ProductServiceClient#hello(); content:
Caused due to : Service B Exception...
reached fallback on workflow side, reason: status 400 reading ProvisioningServiceProxy#executeOrderAction(Long,Long,String)
@FeignClient( url = "/executeOrder", fallbackFactory = TestServiceAFallback.class )
public interface TestServiceA extends Serializable{
@PostMapping( value = "order/{requestId}/order/{orderId}/{command}" )
public ResponseEntity<ProcessInstanceVariable> executeOrderAction( @PathVariable( name = "command" ) String command );
}
@PostMapping( value = /executeOrder )
public ResponseEntity<ProcessInstanceVariable> executeOrderAction( @PathVariable( value = "command" ) String command )
{ //switch code to check the command value and throw exception for one particular command
throw new ValidationException("validation exception from service B");
}
@ExceptionHandler( ValidationException.class )
public ResponseEntity<Object> handleValidationException( ValidationException ve )
{
return new ResponseEntity<>( ve.getMessage(), HttpStatus.BAD_REQUEST );
}
feign.hystrix.enabled=true