membersound
membersound

Reputation: 86667

How to write annotation-based servlet mapping in Spring?

I'd like to rewrite the following xml definition to annotation-based java classes:

  <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>/services/*</url-pattern>
  </servlet-mapping>

The following does not work:

@Service
@RequestMapping(value = "services/*")
public class CXFStartServlet extends CXFServlet {

}

Result:

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NullPointerException
    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.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:485)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Is it impossible?

Upvotes: 0

Views: 1013

Answers (2)

perbellinio
perbellinio

Reputation: 682

You probably forgot to add the import for CXF in your spring configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  ...
</beans>

You can take a look at embedding-cxf-inside-spring and JAX-RS_Spring_CXF for the specification and an example.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

Spring doesn't allow defining servlets. It allows defining controllers, used by the Spring dispatcher servlet.

If you want a servlet, and no XML to map it, you simply need to use the WebServlet annotation of the standard servlet API:

@WebServlet("/services")
public class CXFStartServlet extends CXFServlet {
    ...
}

Upvotes: 2

Related Questions