How can I register a secondary servlet with Spring Boot?

后端 未结 7 677
感动是毒
感动是毒 2020-11-30 19:29

I have an extra servlet I need to register in my application. However with Spring Boot and its Java Config, I can\'t just add servlet mappings in a web.xml file

相关标签:
7条回答
  • 2020-11-30 19:50

    Also available in the BeanDefinitionRegistryPostProcessor

    package bj;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
    import org.springframework.beans.factory.support.RootBeanDefinition;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @SpringBootApplication
    class App implements BeanDefinitionRegistryPostProcessor {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
            registry.registerBeanDefinition("myServlet", new RootBeanDefinition(ServletRegistrationBean.class,
                    () -> new ServletRegistrationBean<>(new HttpServlet() {
                        @Override
                        protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
                            resp.getWriter().write("hello world");
                        }
                    }, "/foo/*")));
        }
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        }
    }
    
    0 讨论(0)
  • 2020-11-30 19:54

    You can register multiple different servlet with different ServletRegistrationBean like @Bean in Application class and you can register a servlet has multiple servlet mapping;

       @Bean
       public ServletRegistrationBean axisServletRegistrationBean() {
          ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
          registration.addUrlMappings("*.jws");
          return registration;
       }
    
       @Bean
       public ServletRegistrationBean adminServletRegistrationBean() {
          return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet");
       }
    
    0 讨论(0)
  • 2020-11-30 20:05

    If you're using embedded server, you can annotate with @WebServlet your servlet class:

    @WebServlet(urlPatterns = "/example")
    public class ExampleServlet extends HttpServlet
    

    From @WebServlet:

    Annotation used to declare a servlet.

    This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.

    And enable @ServletComponentScan on a base class:

    @ServletComponentScan
    @EntityScan(basePackageClasses = { ExampleApp.class, Jsr310JpaConverters.class })
    @SpringBootApplication
    public class ExampleApp 
    

    Please note that @ServletComponentScan will work only with embedded server:

    Enables scanning for Servlet components (filters, servlets, and listeners). Scanning is only performed when using an embedded web server.

    More info: The @ServletComponentScan Annotation in Spring Boot

    0 讨论(0)
  • 2020-11-30 20:06

    This way worked for me, having a servlet called WS01455501EndpointFor89

    @Bean
    public ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBeanAlt(ApplicationContext context) {
        ServletRegistrationBean<WS01455501EndpointFor89> servletRegistrationBean = new ServletRegistrationBean<>(new WS01455501EndpointFor89(),
                "/WS01455501Endpoint");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }
    
    0 讨论(0)
  • 2020-11-30 20:09

    We can also register the Servlet as follow way:

    @Configuration
    public class ConfigureWeb implements ServletContextInitializer, EmbeddedServletContainerCustomizer {
    
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          registerServlet(servletContext);
      }
    
      private void registerServlet(ServletContext servletContext) {
          log.debug("register Servlet");
          ServletRegistration.Dynamic serviceServlet = servletContext.addServlet("ServiceConnect", new ServiceServlet());
    
          serviceServlet.addMapping("/api/ServiceConnect/*");
          serviceServlet.setAsyncSupported(true);
          serviceServlet.setLoadOnStartup(2);
      }
    }
    
    0 讨论(0)
  • 2020-11-30 20:11

    Just add a bean for the servlet. It'll get mapped to /{beanName}/.

    @Bean
    public Servlet foo() {
        return new FooServlet();
    }
    
    0 讨论(0)
提交回复
热议问题