亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SpringBoot - 在 Rest 方法中添加緩存控制頭

SpringBoot - 在 Rest 方法中添加緩存控制頭

30秒到達戰場 2021-11-17 17:28:39
我有一個基本的 SpringBoot 2.0.5.RELEASE 應用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包為可執行 JAR我創建了這個 Rest 方法:  @GetMapping(path = "/users/notifications", consumes = "application/json", produces = "application/json")    public ResponseEntity<List<UserNotification>> userNotifications(            @RequestHeader(value = "Authorization") String authHeader) {        User user = authUserOnPath("/users/notifications", authHeader);        List<UserNotification> menuAlertNotifications = menuService                .getLast365DaysNotificationsByUser(user);        return ResponseEntity.ok(menuAlertNotifications)                .cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS));;    }我想添加一個緩存控制標頭,但我不知道如何...我收到一個編譯錯誤:Multiple markers at this line    - The method cacheControl(CacheControl) is undefined for the type      ResponseEntity<List<UserNotification>>    - CacheControl    - cacheControl我還添加了這個屬性 application.propertiessecurity.headers.cache=false
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

當您使用ResponseEntity.ok(T body)返回類型時,ResponseEntity<T>因為它是一種將數據添加到ResponseEntity.


您需要通過ResponseEntity.ok()沒有返回Builder對象的參數創建的構建器對象。然后,您可以通過 body 方法自己添加數據。


所以你的代碼應該是這樣的


  @GetMapping(path = "/users/notifications", consumes = "application/json", produces = "application/json")

    public ResponseEntity<List<UserNotification>> userNotifications(

            @RequestHeader(value = "Authorization") String authHeader) {


        User user = authUserOnPath("/users/notifications", authHeader);


        List<UserNotification> menuAlertNotifications = menuService

                .getLast365DaysNotificationsByUser(user);



        return ResponseEntity.ok().cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)).body(menuAlertNotifications);



    }


查看完整回答
反對 回復 2021-11-17
  • 1 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號