0

I have a web application (package.jar) which runs with embedded tomcat. Now, I need to add a controller (e.g: http://localhost:8080/package/demo) which will load HTML/javascript/css from an external folder (e.g: /var/www/html/demo) which this web application can read files.

I cannot add them to package.jar as these external files can be updated later and the web-application should return all static contents as they are!

I tried to create a simple RestController which points to /var/www/html/demo/index.html, e.g:

@RestController
public class DemoController {

   @RequestMapping("/")
   public String handle() throws IOException {
      File file = new File("/var/www/html/demo/index.html");
      return FileUtils.readFileToString(file);
}

}

The problem is it cannot request any assets (like: CSS/Javascript files) from /var/www/html/demo/ as they don't exist as resources inside package.jar (e.g: http://localhost:8080/package/assets/js/config.js)

Do you have any suggestions to do it? Thanks

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
  • The right approach is to deploy the static content in webserver and use javascript frameworks like jquery angular js etc to communicate with webservice deployed in app server. – Vipin CP Jun 19 '19 at 12:48

1 Answers1

0

Ok, the idea is it needs to add static resource when Spring Boot starts like this

@SpringBootApplication
public class ApplicationMain extends SpringBootServletInitializer {
    ... 

    @Bean
    WebMvcConfigurer configurer () {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers (ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/demo/**").
                          addResourceLocations("file:///var/www/html/demo/");
            }
        };
    }
}

Then, access http://localhost:8080/package/demo will show the html page with all static content.

Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50