我想将现有的web.xml配置迁移到spring-boot.
但是如何正确完成呢?
这是web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/soap/*</url-pattern>
</servlet-mapping>
如何将其转换为基于注释的spring-boot配置?我尝试了以下方法:
@Configuration
@EnableAutoConfiguration
public class AppConfig extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppConfig.class);
}
@Bean
public CXFServlet cxf() {
return new CXFServlet();
}
}
但是在tomcat上启动时,会引发以下异常:
Caused by: java.lang.NullPointerException: null
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.destroy(CXFNonSpringServlet.java:184)
at org.apache.cxf.transport.servlet.CXFServlet.onApplicationEvent(CXFServlet.java:166)
at org.apache.cxf.transport.servlet.CXFServlet.onApplicationEvent(CXFServlet.java:41)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:333)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:776)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:142)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:485)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89)
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5444)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
解决方法:
您应该为CXFServlet使用ServletRegistrationBean.这将确保它已在servlet容器中注册,并允许您配置URL模式等.例如:
@Bean
public ServletRegistrationBean cxf() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}