java - 带有 ResponseEntity 类型的 spring 表达式
问题描述
实际上,我正在使用 Spring Cache 缓存一个 Http 响应,现在我想设置一个条件,即仅在响应有效时才更新缓存。
@Cacheable(value = CACHE, condition = "#result.body.responseData.toLowerCase().contains("A")")
public ResponseEntity<ProcessMqReqPostResponseBody> sendMqRequest(Integer pageNumber, Integer pageSize, String sortOrder, String merchantId) {
//Method Implementation
}
没有这个条件,我可以很好地测试我的缓存,但是当我添加这个条件时,我得到了错误
org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“body”
我不明白,因为我可以在我的测试中输出 responseEntity 并且它不为空。这种行为正确吗?
谢谢,阿什利
解决方案
Spring 的行为是正确的,因为您正在编写一个将根据结果起作用的条件。
该#result
值始终null
在方法执行之前,结果值仅在方法执行后填充。
如果你想测试这个,你可以改变你的条件,方法根本不会被缓存,也不会出错。
@Cacheable(value = CACHE, condition = "#result != null and #result.body.responseData.toLowerCase().contains(\"A\")")
如果您想对结果采取行动,您只能使用unless
元素来执行此操作。
与 condition() 不同,此表达式在方法被调用后进行评估,因此可以引用结果。
推荐阅读
- user-interface - 关于弹出框/抽屉菜单中常见UI设计元素的问题
- docker - 无法连接到主机上映射的 docker 容器端口
- peewee - 从 Peewee 中的组合查询中选择列
- python-3.x - 如何在 python 中更新 spark 数据框的值?
- angular - 将数据从 httpget 转换为数据数组
- python - 计算熊猫数据框中列值之间的距离
- solr - 索尔 | 获取用户搜索最多的查询并使用 GSA 的自动建议功能
- javascript - MapBox GL:“Map.Off”问题
- symfony - 无法使用 Symfony 2.8 的作曲家加载供应商依赖项
- http - 如何使用 HTTP/1.5 协议请求 URL?