Skip to main content

Using the servlet transport without Spring

A user who doesn't want to use the Spring libraries can alternatively publish the endpoint with CXF servlet transport. To do this, extend the CXFNonSpringServlet and then override the method loadBus, e.g.:

@Override
public void loadBus(ServletConfig servletConfig) 
   throws ServletException {
   super.loadBus(servletConfig);        
        
   // You could add the endpoint publish codes here
   Bus bus = cxf.getBus();
   BusFactory.setDefaultBus(bus); 
   Endpoint.publish("/Greeter", new GreeterImpl());
        
   // You can als use the simple frontend API to do this
   ServerFactoryBean factroy = new ServerFactoryBean();
   factory.setBus(bus);
   factory.setServiceClass(GreeterImpl.class);
   factory.setAddress("/Greeter");
   factory.create();              
}

If you are using Jetty as the embedded servlet engine, the endpoint can be published as follows:

// Set up the system properties to use 
// the CXFBusFactory not the SpringBusFactory
String busFactory = 
System.getProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME, 
   "org.apache.cxf.bus.CXFBusFactory");
try {
   // Start up the jetty embedded server
   httpServer = new Server(9000);
   ContextHandlerCollection contexts 
      = new ContextHandlerCollection();
   httpServer.setHandler(contexts);
            
   Context root = new Context(contexts, "/", Context.SESSIONS);
            
   CXFNonSpringServlet cxf = new CXFNonSpringServlet();
   ServletHolder servlet = new ServletHolder(cxf);
   servlet.setName("soap");
   servlet.setForcedPath("soap");
   root.addServlet(servlet, "/soap/*");
            
   httpServer.start();
           
   Bus bus = cxf.getBus();
   setBus(bus);
   BusFactory.setDefaultBus(bus);
   GreeterImpl impl = new GreeterImpl();
   Endpoint.publish("/Greeter", impl);
} catch (Exception e) {
   throw new RuntimeException(e);
} finally {
   // clean up the system properties
   if (busFactory != null) {
      System.setProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME, 
      busFactory);
   } else {
      System.clearProperty(BusFactory.BUS_FACTORY_PROPERTY_NAME);
   }
}

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!