2016-08-24 10 views
2

私はgradleプロジェクトを持っています。私はバックエンドとフロントエンドの角2に対してスプリングブートを使用しています。 追加または削除後に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();  
} 
+1

'spring.cache.type'はスプリングキャッシングをサポートしています。それはあなたが後になっているキャッシングヘッダーとは関係がありません... –

+0

別の方法がありますか?値noneはそれから何を意味しますか?私はここでこれを読んだ:http://stackoverflow.com/questions/35917159/spring-boot-how-to-disable-cachable-during-development。実際にはこれはあなたの投稿です) – trap

+0

言い換えれば、Springキャッシュのためには、ヘッダをキャッシュしません。 –

答えて

0

M. Deinumが述べたように、spring.cache.typeはキャッシングサポート用です。これはまったく別のバネ機能です。

しかしSpring docsによると、1はキャッシュ制御を有効にするには、このJavaの設定を使用することができます。

@EnableWebSecurity 
public class WebSecurityConfig extends 
WebSecurityConfigurerAdapter { 

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

これは、自動的に必要なキャッシュ制御ヘッダを追加します。

関連する問題