Can I enable the tomcat manager app for Spring Boot's embedded tomcat?

后端 未结 3 631
旧巷少年郎
旧巷少年郎 2020-12-11 09:21

I\'m doing a research project to determine to what extent we can configure embedded Tomcat with Spring boot. One of the items I was asked to look into was with regard to whe

相关标签:
3条回答
  • 2020-12-11 09:25

    There is a way how to convert Spring Boot's JAR to WAR. After that you can use your hosted tomcat manager.

    You need to say to Maven/Gradle to cuild WAR instead of JAR and create this basic servet configuration

    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.web.SpringBootServletInitializer;
    
    public class HelloWebXml extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    }
    
    0 讨论(0)
  • 2020-12-11 09:38

    If all you need is a list of endpoints, you could configure your application to generate swagger pages. I use spring boot and springfox.

    API Docs:
    http://localhost:8080/v2/api-docs

    Swagger UI:
    http://localhost:8080/swagger-ui.html

    Tutorial:
    Springfox Reference Documentation

    Code:

    package some.package;
    
    import javax.servlet.ServletContext;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.paths.RelativePathProvider;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Value("${swagger.host:}")
        private String swaggerHost;
    
        @Value("${swagger.basePath:/}")
        private String swaggerBasePath;
    
        @Autowired
        ServletContext servletContext;
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                .host(swaggerHost)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return swaggerBasePath;
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.basePackage("path.to.package"))
                .build();
        }
    }
    
    0 讨论(0)
  • 2020-12-11 09:48

    Does the embedded tomcat 7 used by Spring Boot contain a tomcat manager app

    No, it doesn't and I'm not really sure that it makes sense to try to add it.

    A primary function of the manager app is to allow you to start and stop individual applications without stopping the container and to deploy and undeploy individual applications. When you're using Spring Boot with embedded Tomcat you should consider your application and container as a single entity so starting or stopping your application and starting and stopping the container are one and the same thing.

    A secondary function of the manager app is to provide access to OS and JVM properties, something that Spring Boot's actuator already does for you.

    What does it take to add/enable it?

    If you choose not to heed the above, it's easy to add the manager app (although I can't promise that it all works as expected – I leave that as an exercise for the (foolhardy) reader):

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        return new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                    Tomcat tomcat) {
                tomcat.addUser("admin", "secret");
                tomcat.addRole("admin", "manager-gui");
    
                try {
                    tomcat.addWebapp("/manager", "/path/to/manager/app");
                }
                catch (ServletException ex) {
                    throw new IllegalStateException("Failed to add manager app", ex);
                }
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }
        };
    }
    

    You'll also need a dependency on Jasper as the manager app uses JSPs. Assuming you're using Maven:

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题