Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - replace default embedded Tomcat connector

Tags:

spring-boot

I need to add an AJP connector to embedded Tomcat and disable (or replace) the default connector that listens on 8080.

I've tried customizing this with EmbeddedServletContainerCustomizer, but I can't get a handle on the Tomcat object to replace the default connector created there. As a result I end up with the http port on 8080 in addition to my AJP ports.

Next, I've tried extending TomcatEmbeddedServletContainerFactory and overriding its getTomcatEmbeddedServletContainer method. Per the JavaDoc, this appears to be the perfect place to replace the default connector, but it still ends up being enabled (and doesn't create my AJP connector either). Any ideas what I might be missing? I've verified with the debugger that my configuration is being run.

Per answer below, here's the cleanest solution:

@Bean
public EmbeddedServletContainerFactory tomcat() {
    TomcatEmbeddedServletContainerFactory myFactory = new TomcatEmbeddedServletContainerFactory();
    myFactory.setProtocol("AJP/1.3");
    myFactory.setPort(9000);
    return myFactory;
}

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer2() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                @Override
                public void customize(Connector connector) {
                    connector.setRedirectPort(9001);
                }
            });
        }
    };
} 
like image 894
gyoder Avatar asked Jan 20 '15 16:01

gyoder


People also ask

Can we replace the embedded Tomcat server in spring boot?

The Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot.

How do I change the default embedded Tomcat server in spring boot?

Another way to change the port of embedded tomcat in the Spring Boot application is by specifying the server. port property in the resource file. For example, if you want your Spring boot application to listen on port 8080, then you can specify server. port=8080 on the application.

Can we override or replace the embedded Tomcat server in spring boot Mcq?

Can we override or replace the Embedded tomcat server in Spring Boot? Yes, we can replace the Embedded Tomcat server with any server by using the Starter dependency in the pom.


1 Answers

You can use a TomcatConnectorCustomizer to configure the existing connector to use AJP by adding it to the TomcatEmbeddedServletContainerFactory.

like image 147
Andy Wilkinson Avatar answered Oct 07 '22 17:10

Andy Wilkinson