3

I have a gradle project and I am using spring-boot in my backend and for the front-end Angular 2. Every time I do a get-Request after add or delete, the IE does not call the get-method. He takes the array from the cache.

I have already found a solution client side. I have put the headers:

        'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': '0'

in my Request. And then it works.

But I tried to solve this problem server side. I found in the internet, that you can prevent caching if you just add

spring.cache.type=NONE

in your application.properties. But this is not working.

Did I forgot anything else?

Application:

@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:

@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();    
}
trap
  • 2,550
  • 7
  • 21
  • 42
  • 1
    `spring.cache.type` is for spring Caching support. It has no relation to the caching headers you are after... – M. Deinum Aug 24 '16 at 10:11
  • is there another way? what does the value none means then? I have read this here: http://stackoverflow.com/questions/35917159/spring-boot-how-to-disable-cachable-during-development. In fact this is your post ;) – trap Aug 24 '16 at 10:14
  • As mention it is for Spring Cache not for caching headers... – M. Deinum Aug 24 '16 at 10:26
  • You can't fix this on the backend, as the caching is happening in the browser. Look into the docs on the cache-related headers you added. – JudgingNotJudging Aug 24 '16 at 14:51

1 Answers1

0

As M. Deinum mentioned, spring.cache.type is for caching support which is entirely another spring feature.

However according to Spring docs, one can use this java configuration to enable cache-control:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        // ...
        .headers().cacheControl();
    }
}

This will automatically add the necessary cache-control headers.

emrekgn
  • 624
  • 9
  • 25