0

I use AngularJS at frontend and Spring Boot and Security in Backend. The frontend structure looks like this:

frontend

and in Spring I have this classes:

backend

The class MvcConfigurer currently looks like this:

@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }
}

My question now is:

What do I have to write into the addResourceHandlers method in order to enable caching at Browser frontend.

Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
quma
  • 5,233
  • 26
  • 80
  • 146

1 Answers1

0

In addResourceHandlers you should register the application resources, stored in the WAR file, that needs to be served.

Here is an example of a JavaScript files resolver:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/js/**").addResourceLocations("../webapp/js/");
}

Also, you can append .setCachePeriod( time_in_seconds ) so the browser will cache this resolvers in order to optimize request handling.

Please read more details in this article by Baeldung. I think you'll find it quite useful.

Tome Pejoski
  • 1,582
  • 2
  • 10
  • 34
  • Thanks a lot Tome for your answer, it was really helpful. I would have one more question, In my Spring Boot final jar I have a lib folder with a lot of jars, one jar is called e.g. A.jar where the class **MvcConfigurer** is in it and the other jar is called B.jar where the frontend files are in it. My question now would be how to set the path for **registry.addResourceHandler("/js/**").addResourceLocations("../webapp/js/");** Is ist possible also to specify the the jar name in the path? – quma Nov 16 '16 at 07:59
  • Regarding your additional question, here is a similar existing Stack Overflow question http://stackoverflow.com/questions/25061237/spring-4-addresourcehandlers-not-resolving-the-static-resources. – Tome Pejoski Nov 16 '16 at 08:35
  • I would also recommend you to watch some of the courses by Jeva Brains https://javabrains.io. He is quite good in explaining the basics of setting up an application with Java/Spring and also using correctly the other tools like Maven, Hibernate, etc. – Tome Pejoski Nov 16 '16 at 08:37