Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot static files load slowly

I have a spring boot application. When I deploy this to a remote server and run it using the embedded tomcat, my static files are loading painfully slow. For example, it takes about 7 seconds to load a 50-line js file. It's a pretty standard Spring MVC application. My main just looks like this

package com.mineiq;

import javax.servlet.MultipartConfigElement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("128MB");
        factory.setMaxRequestSize("128MB");
        return factory.createMultipartConfig();
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And, for example, my home page is just serving a static html page, so there's almost no server-side load. I tried disabling spring-security, but it didn't help.

This only happens when I run the application on my remote server. Locally, everything is fast.

I configured nginx separately to serve the static files on a different port, and it serves them very quickly, so the problem is not with the server itself. This was just a test (not a solution), as I don't really want to have to run a separate server for static files.

Thanks for any help.

like image 290
user3812797 Avatar asked Dec 06 '25 16:12

user3812797


1 Answers

Some tips that you can check:

First try hitting the resource directly in the url.

Make sure that you have not enabled the default servlet handler in your app (http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html#mvc-default-servlet-handler) so that the static resources are not being served by the container’s default Servlet.

By default Spring Boot will serve static content from a folder called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServeltContext. It uses the ResourceHttpRequestHandler which optimizes the static content delivery by provided cache settings.

If you want to add other locations for static content other than the above default ones you can do that by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method like below:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.examples.mvc.base.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
    }

}

More info:

  • http://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
  • http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/
  • http://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
like image 107
Ravi Kadaboina Avatar answered Dec 08 '25 08:12

Ravi Kadaboina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!