2014-03-13 161 views
15

我已經在web.xml中一個簡單的servlet配置:如何在web.xml中配置spring-boot servlet?

<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class> 
    <init-param> 
     <param-name>org.atmosphere.servlet</param-name> 
     <param-value>org.springframework.web.servlet.DispatcherServlet</param-value> 
    </init-param> 
    <init-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </init-param> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>net.org.selector.animals.config.ComponentConfiguration</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    <async-supported>true</async-supported> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

我怎樣才能把它改寫爲SpringBootServletInitializer?

+0

目前尚不清楚你正在嘗試做什麼。如果你想要使用Servlet 3.0的完全相同的Spring應用程序,爲什麼要定義不同的Servlet類型(MeteorServlet vs. DispatcherServlet)?您在web.xml中加載的配置類沒有明確地在任何地方使用。你也擴展了'SpringBootServletInitializer',但是似乎沒有覆蓋關鍵的'configure'方法。 –

+0

對不起,也許是你誤入歧途的一個例子。我需要配置Meteor Servlet,如web.xml – Selector

+0

中所述。抱歉,沒有關注。 web.xml有一個'DispatcherServlet'。如果你希望另一種servlet作爲默認的servlet使用答案中的代碼,但使用不同的servlet類(儘管bean名稱仍然是「DispatcherServlet」)。 –

回答

23

如果我把你的面值的問題(你想有一個SpringBootServletInitializer是複製現有的應用程序),我想這將是這個樣子:

@Configuration 
public class Restbucks extends SpringBootServletInitializer { 

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.sources(Restbucks.class, ComponentConfiguration.class); 
    } 

    @Bean 
    public MeteorServlet dispatcherServlet() { 
     return new MeteorServlet(); 
    } 

    @Bean 
    public ServletRegistrationBean dispatcherServletRegistration() { 
     ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet()); 
     Map<String,String> params = new HashMap<String,String>(); 
     params.put("org.atmosphere.servlet","org.springframework.web.servlet.DispatcherServlet"); 
     params.put("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext"); 
     params.put("contextConfigLocation","net.org.selector.animals.config.ComponentConfiguration"); 
     registration.setInitParameters(params); 
     return registration; 
    } 

} 

更多細節見docs on converting an existing app

但是,現在使用Tomcat和Spring中的原生Websocket支持(請參閱websocket sampleguide作爲示例),您可能比使用Atmosphere更好。

+0

其他url映射使用registration.addUrlMappings(「/ whatever/*」,「/ whatever2/*」); – Bamboomy

+0

我在研究如何在Spring Boot應用程序中嘗試允許匿名訪問命名的servlet後產生錯誤消息時,如何定義Spring Boot servlet的篩選器列表時發現了這篇文章。你願意發表評論嗎?這裏是鏈接:http://stackoverflow.com/questions/36489253/api-url-has-an-empty-filter-list-in-spring-boot-security – CodeMed