How to migrate from SOAP CXF web.xml config to Spring-Boot?

I want to migrate my existing configuration web.xml

to spring-boot

.

But how to do it right?

This is 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>

      

How do I convert this to annotation based on spring-boot config? I tried the following:   

@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();
    }   
}

      

But when running on tomcat, the following exception is thrown:

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)

      

+3


source to share


1 answer


You must use ServletRegistrationBean

for CXFServlet

. This will ensure that it is registered in the servlet container and allows you to customize the URL pattern, etc. For example:



@Bean
public ServletRegistrationBean cxf() {
    return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}

      

+3


source







All Articles