2
我有一個gradle項目,我在後端和前端Angular 2中使用了spring-boot。 每次我在添加或刪除操作後執行get-Request時,IE不調用get方法。他從緩存中獲取數組。如何防止彈簧啓動中的緩存
我已經找到了解決方案客戶端。我已經把標題:
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
在我的要求。然後它工作。
但我試圖解決這個問題的服務器端。我發現在互聯網上,你可以防止緩存,如果你只是在你的application.properties中添加
spring.cache.type=NONE
。但是這不起作用。
我忘了其他什麼嗎?
應用:
@SpringBootApplication
public class MyApplicationextends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
UIController:
@RequestMapping("/service")
public interface MyUIController {
@RequestMapping(method=RequestMethod.GET, value= "/getBooks", produces="application/json")
public List<Books> getBooks(HttpServletRequest request, HttpServletResponse response);
}
RestController:
@RestController
public class MyRestController implements MyUIController {
public List<DomainEntity> getDomains(HttpServletRequest request, HttpServletResponse response) {
return myUIService.getBookss(request, response);
服務:
@Service
public class MyUIService {
public List<Books> getBooks(HttpServletRequest request, HttpServletResponse response) {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<List<Books>> responseEntity = restTemplate.exchange(
UriComponentsBuilder.fromHttpUrl(url).build().encode().toUri(),
HttpMethod.GET,
requestEntity,
new ParameterizedTypeReference<List<Books>>(){});
return responseEntity.getBody();
}
'spring.cache.type'用於彈簧緩存支持。它與你之後的緩存標題無關... –
還有另一種方法嗎?那麼這個價值意味着什麼呢?我已閱讀此處:http://stackoverflow.com/questions/35917159/spring-boot-how-to-disable-cachable-during-development。事實上,這是你的文章;) – trap
正如所提到的,它是用於Spring Cache不用於緩存標頭...... –