首页 > 解决方案 > 请求范围 bean 不能在响应正文中使用 [Spring Boot]

问题描述

我有一个 Request scoped(prototype) bean 作为 Rest Controller 的 Response 并得到 Exception ,不工作

我按照错误消息“类型定义错误:[简单类型,类 org.springframework.context.expression.StandardBeanExpressionResolver];嵌套异常是 com.fasterxml.jackson .databind.exc.InvalidDefinitionException:没有为类 org.springframework.context.expression.StandardBeanExpressionResolver 找到序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:com.epic.dcms.kyc .beans.MainResponse$$EnhancerBySpringCGLIB$$82085c99[\"targetSource\"]->org.springframework.aop.target.SimpleBeanTargetSource[\"beanFactory\"]->org.springframework.beans.factory.support.DefaultListableBeanFactory[\ "beanExpressionResolver\"])",

没有工作然后有一个循环依赖并抛出异常

@RequestScope 注释到 @scope 注释更改也没有解决问题

我注意到只有当 MainResponse 用作响应时才会出现此异常。

  Bean
        @RequestScope
        @Component
        public class MainResponse implements Serializable{
        //fields and getters and setters
        }

  Controller class
         @PostMapping("/uploadFile")
            public MainResponse uploadFile(@RequestParam("file") MultipartFile file) throws FileStorageException {
                String fileName = fileStorageService.storeFile(file);

                response.putPayLoad(true,"fileName",fileName)
                        .putPayLoad("ContentType",file.getContentType())
                        .putPayLoad("size",file.getSize());

                return response;
            }

当我从 MainResponse bean 中删除 @RequestScope 时,它​​工作正常,知道如何解决这个问题吗?

标签: javaspring-bootrequestscope

解决方案


无需使用“@RequestScope”标记您的响应类,数据绑定将由框架本身管理。


推荐阅读