简体   繁体   English

请求范围的bean不能在Response正文中使用[Spring Boot]

[英]Request Scoped bean cannot use in Response body [Spring Boot]

I have a Request scoped(prototype) bean as a Response for a Rest Controller and getting Exception , not working 我有一个Request作用域(原型)bean作为Rest Controller的响应,并获取Exception,但不起作用

I tried spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false on the property file as suggested by the error message "Type definition error: [simple type, class org.springframework.context.expression.StandardBeanExpressionResolver]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.epic.dcms.kyc.beans.MainResponse$$EnhancerBySpringCGLIB$$82085c99[\\"targetSource\\"]->org.springframework.aop.target.SimpleBeanTargetSource[\\"beanFactory\\"]->org.springframework.beans.factory.support.DefaultListableBeanFactory[\\"beanExpressionResolver\\"])" , 我按照错误消息“类型定义错误:[简单类型,类org.springframework.context.expression.StandardBeanExpressionResolver]”的建议在属性文件上尝试了spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false ;嵌套异常为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 \\”])“

didn't work then there was a cyclic dependency and exception was thrown 没有工作,然后有一个周期性的依赖关系,并引发了异常

@RequestScope annotation to @scope annotation change didn't solve the issue also 将@RequestScope注释更改为@scope注释也不能解决问题

I notice that this exception only comes when the MainResponse used as a Response. 我注意到只有在MainResponse用作Response时才会出现此异常。

  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;
            }

when I remove @RequestScope from MainResponse bean it just works fine, any idea how to fix this?? 当我从MainResponse bean中删除@RequestScope时,它工作正常,任何想法如何解决此问题?

无需将响应类标记为“ @RequestScope”,数据绑定将由框架本身进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM