java - 请求范围 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 时,它工作正常,知道如何解决这个问题吗?
解决方案
无需使用“@RequestScope”标记您的响应类,数据绑定将由框架本身管理。
推荐阅读
- asp.net - 录制过程中文件上传失败
- c - Gnu Make >$* 和 $* 的含义
- android - Nativescript:如何从文件路径创建一个 JS File 对象?
- python - 为什么从 /sys/bus/... 读取需要这么多时间?
- django - 使用 django 消息框架发送列表
- git - 我可以在没有 Git 的情况下创建 vue-cli 项目吗?
- node.js - Docusign 沙盒 | 总是返回 invalid_grant 错误 | 授权代码授予
- ansible - ansible将变量写入本地文件
- java - 如何让我的二叉树插入函数工作?
- mysql - 为什么mysql escape会破坏整个查询?