@jasen
To enable browser caching in a Spring Boot application, you can leverage Spring Boot's caching mechanisms along with setting appropriate headers in the response. Here is how you can do it:
1 2 3 4 5 6 7 |
@SpringBootApplication @EnableCaching public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } |
1 2 3 4 5 6 7 8 |
@Service public class MyService { @Cacheable("myCache") public String getSomeData() { // Method implementation } } |
With this configuration, Spring Boot will cache the result of the getSomeData
method in the myCache
cache.
1 2 3 4 5 6 7 8 9 |
@Component public class CacheControlFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { response.setHeader("Cache-Control", "max-age=3600"); // Set cache expiration time to 1 hour filterChain.doFilter(request, response); } } |
With these steps, you can enable browser caching in your Spring Boot application to improve performance by caching static resources on the client side.