如何在Spring Boot中发送请求范围bean作为响应



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

@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]
.....
.....

请帮忙,提前谢谢!!

这是因为OrderDTOOrderResponse都是序列化对象。这会触发来自Jackson的自参考错误。

您可以将ObjectMapperbean配置为禁用此功能。

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);

最新更新