I would need to send the response which also consists of request information as well. Tried with the following code but getting below exception:
@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
}
Controller & Advice classes:
@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);
}
}
Getting below error:
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]
.....
.....
Please help on this, Thanks in advance!!
This happens because both
OrderDTO
andOrderResponse
are serialized objects. This triggers the self reference error fromJackson
.You can configure your
ObjectMapper
bean to disable this.