Michael  Shogun
Michael Shogun

Reputation: 13

Multiple DispatcherServlet - Spring-Boot-2.4

I'm trying to create multiple dispatcherservlet ("/rest/", "/jsp/", "mq/*"). Springboot initialises only one dispatcherservlet.

i have two bean creation methods for DispatcherServletRegistrationBean in order to create two dispatcher servlet. I has set the order and precedence level for both beans. When i start the application, only one of the dispatcherservlet is getting Initialised. In this case, it is "restDisparcher" (please look for console output). what should i do in order to setup multiple dispatcher servlet.

@SpringBootConfiguration
public class AppConfiguration {

    @Bean
    @Primary
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBeanRest() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet(new AnnotationConfigServletWebApplicationContext("com.michael.springsecurityentitlement.rest"));
        DispatcherServletRegistrationBean dispatcher = new DispatcherServletRegistrationBean(dispatcherServlet , "/rest/*");
        dispatcher.setName("restDispatcher");
        dispatcher.setLoadOnStartup(1);
        dispatcher.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return dispatcher;
    }
    
    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBeanJsp() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet(new AnnotationConfigServletWebApplicationContext("com.michael.springsecurityentitlement.jsp"));
        DispatcherServletRegistrationBean dispatcher = new DispatcherServletRegistrationBean(dispatcherServlet , "/jsp/*");
        dispatcher.setName("restDispatcher");
        dispatcher.setLoadOnStartup(1);
        dispatcher.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return dispatcher;
    }
    
    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory("/custom",8081);
        return tomcatServletWebServerFactory;
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(AppConfiguration.class);
        app.run(args);      
    }

}

Console:

2021-03-04 12:09:39.015  INFO 1108 --- [           main] c.m.s.AppConfiguration                   : Starting AppConfiguration using Java 15.0.2 on ASINTHs-MacBook-Pro.local with PID 1108 (/Users/asinth/git/spring-security-entitlement/target/classes started by asinth in /Users/asinth/git/spring-security-entitlement)
2021-03-04 12:09:39.030  INFO 1108 --- [           main] c.m.s.AppConfiguration                   : No active profile set, falling back to default profiles: default
2021-03-04 12:09:39.539  INFO 1108 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2021-03-04 12:09:39.551  INFO 1108 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-04 12:09:39.551  INFO 1108 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-04 12:09:39.704  INFO 1108 --- [           main] o.a.c.c.C.[.[localhost].[/custom]        : Initializing Spring embedded WebApplicationContext
2021-03-04 12:09:39.704  INFO 1108 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 635 ms
2021-03-04 12:09:39.806  INFO 1108 --- [           main] o.s.boot.web.servlet.RegistrationBean    : Servlet restDispatcher was not registered (possibly already registered?)
2021-03-04 12:09:39.880  INFO 1108 --- [           main] o.a.c.c.C.[.[localhost].[/custom]        : Initializing Spring DispatcherServlet 'restDispatcher'
2021-03-04 12:09:39.881  INFO 1108 --- [           main] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'restDispatcher'
2021-03-04 12:09:40.131  INFO 1108 --- [           main] o.s.web.servlet.DispatcherServlet        : Completed initialization in 250 ms
2021-03-04 12:09:40.136  INFO 1108 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path '/custom'
2021-03-04 12:09:40.155  INFO 1108 --- [           main] c.m.s.AppConfiguration                   : Started AppConfiguration in 2.093 seconds (JVM running for 3.252)

Upvotes: 1

Views: 1849

Answers (1)

Wafo
Wafo

Reputation: 54

I think the problem is your dispatcher name. Set different names for different Dispatcher and it should works. Maybe like this :

@SpringBootApplication
public class AppConfiguration {
    @Bean
    @Primary
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBeanRest() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet(new AnnotationConfigServletWebApplicationContext("com.michael.springsecurityentitlement.rest"));
        DispatcherServletRegistrationBean dispatcher = new DispatcherServletRegistrationBean(dispatcherServlet , "/rest/*");
        dispatcher.setName("restDispatcher");
        dispatcher.setLoadOnStartup(1);
        dispatcher.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return dispatcher;
    }

    @Bean
    public DispatcherServletRegistrationBean dispatcherServletRegistrationBeanJsp() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet(new AnnotationConfigServletWebApplicationContext("com.michael.springsecurityentitlement.jsp"));
        DispatcherServletRegistrationBean dispatcher = new DispatcherServletRegistrationBean(dispatcherServlet , "/jsp/*");
        dispatcher.setName("jspDispatcher");
        dispatcher.setLoadOnStartup(1);
        dispatcher.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return dispatcher;
    }

    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory("/custom",8081);
        return tomcatServletWebServerFactory;
    }

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

}

Upvotes: 1

Related Questions