编程语言
首页 > 编程语言> > java – 如何为使用Spring Cache缓存的Spring Web Service设置正确的Last-Modified标头值?

java – 如何为使用Spring Cache缓存的Spring Web Service设置正确的Last-Modified标头值?

作者:互联网

我有这样的Spring MVC控制器:

@Controller
@RequestMapping(value = "/user")
public class UserController {
   .....      
   @Cacheable(value = "users", key = "#id")
   @RequestMapping(value = "/get", method = RequestMethod.GET)
   @ResponseBody
   public User getUser(Long id){
       return userService.get(id);
   }
   ....
}

我想将标题Last-Modified添加到GetUser Web服务的HTTP响应中.
如何在我的商店中添加缓存时获得正确的日期?
如何将此日期的Last-Modified标题添加到Spring Controller方法的响应中?

解决方法:

这个怎么样:

@Controller
@RequestMapping(value = "/user")
class UserController {

    @Cacheable(value = "users", key = "#id")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> getUser(Long id) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Last-Modified", dateFormat.format(new Date()));
        return new ResponseEntity<SecurityProperties.User>(headers, userService.get(id), HttpStatus.OK);
    }
}

标签:java,caching,spring-mvc,spring,spring-cache
来源: https://codeday.me/bug/20190715/1467286.html