【发布时间】:2018-09-12 17:49:58
【问题描述】:
有没有办法通过方法参数缓存spring REST API的响应?例如,在下面的代码中,如果国家/地区已经检索过一次,则从缓存中返回相同的响应(json 序列化数据)。
@Controller
public class DataController {
// Can we cache here by country?
@RequestMapping(value = "/api/info/{country}", method = RequestMethod.GET)
public CountryInfo getCountryInfo(@PathVariable("country")String country){
return service.getCountryInfo(country);
}
}
【问题讨论】:
-
你可以,不过最好加个Spring缓存机制,在service方法里加
@Cacheable就行了。 -
@daniu 为什么?在控制器中缓存,我希望还能节省序列化成本。
-
在到达您发布的代码时,序列化仍然会发生(即将生成的
CountryInfo编码为 JSON)。
标签: java spring-mvc caching