0

我需要发送也包含请求信息的响应。尝试使用以下代码,但出现以下异常:

@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class OrderResponse {
     private String discountDetails;
     private boolean confirmedStatus;
     private OrderDto orderDetails;
     private String shortMsg;
     private int status;
     //Getter & Setters
}

public class OrderDto {
     private int quantity;
     private String product;
     //Getters & Setters
}

控制器和建议类:

@RestController
    @RequestMapping(value = "/orders")
    public class OrderController {
         @Autowired
         private OrderResponse orderResponse;
         ....
         @PostMapping(value = "/order/placeOrder")
         public ResponseEntity<OrderResponse> placeOrder(@RequestBody OrderDto orderDto){
           ....
           orderResponse.setOrderDetails(orderDto);//Adding request details to the response
           ....
           return new ResponseEntity<>(orderResponse, HttpStatus.OK); 
         }
    }

@RestControllerAdvice(assignableTypes = OrderController.class)
public class OrderExceptionHandler {
     @Autowired
     private OrderResponse orderResponse;

     @ExceptionHandler(value = DataAccessException.class)
     protected ResponseEntity<OrderResponse> constraintHandling(DataAccessException ex) {
        ....
        orderResponse.setShortMsg(shortMsg);
        orderResponse.setStatus(200);
        ....
        return new ResponseEntity<>(orderResponse, HttpStatus.OK);
     }
}

出现以下错误:

2020-05-10 18:39:34.362 ERROR 12620 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.aop.ClassFilter]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: spring.test.jpa.controllers.OrderResponse$$EnhancerBySpringCGLIB$$5deecefd["advisors"]->org.springframework.aop.support.DefaultIntroductionAdvisor[0]->org.springframework.aop.support.DefaultIntroductionAdvisor["classFilter"])] with root cause

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle (through reference chain: spring.test.jpa.controllers.OrderResponse$$EnhancerBySpringCGLIB$$5deecefd["advisors"]->org.springframework.aop.support.DefaultIntroductionAdvisor[0]->org.springframework.aop.support.DefaultIntroductionAdvisor["classFilter"])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.10.3.jar:2.10.3]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.10.3.jar:2.10.3]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._handleSelfReference(BeanPropertyWriter.java:944) ~[jackson-databind-2.10.3.jar:2.10.3]
.....
.....

请帮助解决这个问题,在此先感谢!

4

1 回答 1

0

发生这种情况是因为OrderDTOOrderResponse都是序列化的对象。这会从 触发自引用错误Jackson

你可以配置你的ObjectMapperbean 来禁用它。

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
于 2020-05-10T16:07:24.790 回答