首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring中发送请求作用域bean作为响应

如何在Spring中发送请求作用域bean作为响应
EN

Stack Overflow用户
提问于 2020-05-10 14:02:01
回答 1查看 766关注 0票数 0

我将需要发送的答复,其中也包括要求的信息。尝试使用以下代码,但在异常下面获得:

代码语言:javascript
复制
@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
}

主计长及意见班:

代码语言:javascript
复制
@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);
     }
}

获取以下错误:

代码语言:javascript
复制
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]
.....
.....

请帮忙,谢谢提前!

EN

回答 1

Stack Overflow用户

发布于 2020-05-10 16:07:24

这是因为OrderDTOOrderResponse都是序列化对象。这将触发来自Jackson的自引用错误。

您可以配置您的ObjectMapper bean来禁用它。

代码语言:javascript
复制
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61713269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档