Tomcat built-in list for spring-boot application directory

I have a spring boot application with built-in Tomcat. I wanted to put some image files and folders from another place through the tomcat directory list. So I added the following in the configuration file

public class AppConfig extends WebMvcConfigurerAdapter

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:///xxx/yyy/images/");
    }
}

Now I can access individual images if I know the name.

Example: localhost: 8080 / images / file.jpg.

But since the default directory list is incorrect, I cannot access the image list through "localhost: 8080 / images /" to find out all available images.

I tried the option below to add lists, but didn't work.

public class MyApplication implements ServletContextInitializer{

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

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("listings", "true");
    }
}
+4
source share
2

, SpringBoot Embedded Tomcat JSPServlet Options, EmbeddedServletContainerCustomizer @Bean .

@Bean
public EmbeddedServletContainerCustomizer customizer() {
    return new EmbeddedServletContainerCustomizer() {

        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container instanceof TomcatEmbeddedServletContainerFactory) {
                customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
            }
        }

        private void customizeTomcat(TomcatEmbeddedServletContainerFactory tomcat) {
            tomcat.addContextCustomizers(new TomcatContextCustomizer() {

                @Override
                public void customize(Context context) {
                    Wrapper defServlet = (Wrapper) context.findChild("default");
                    defServlet.addInitParameter("listings", "true");
                }
            });
        }
    };
}

.

0

springboot/** ResourceHttpRequestHandler. DefaultServlet , . Mark, .

  • DefaultServlet → "/static/*"
  • docbase, , tmp. , .

            public void customize(Context context) {
                context.setDocBase("../../../mytest");
                Wrapper defServlet = (Wrapper) context.findChild("default");
                defServlet.addInitParameter("listings", "true");
                defServlet.addInitParameter("readOnly", "false");
                defServlet.addMapping("/static/*");
            }
    

    Deployment folder structure
    /myhome/mytest
    ----myapp.jar
    ----/tomcat/webapps
    ----/static
    --------All static files go here

    application.yml
    server :
     tomcat :
      basedir : tomcat

    Current working dir to run the app /myhome/mytest

    url to test : http://localhost:8080/static
0

Source: https://habr.com/ru/post/1625920/


All Articles