Search code examples
javaspringrestsoapcxf

Expose Rest and Soap for one endpoint simultaneously by Spring and CXF


I have a very simple service which has to be exposed as two way: REST and SOAP I wrote following code:

@WebService(name = "Base")
@Path("/Base")
@Produces("application/json")
public class BaseService
{
    @POST
    @Path("/Hello")
    public String sayHello()
    {
        return  "Hello";
    }
}

That was my endpoint and I configure it as follows:

import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;

@Configuration
public class CxfConfiguration {
    @Bean( destroyMethod = "shutdown" )
    public SpringBus cxf() {
        return new SpringBus();
    }

    @Bean @DependsOn( "cxf" )
    public Server getServer() 
    {
        JaxWsServerFactoryBean wsFactory = new JaxWsServerFactoryBean();
        wsFactory.setServiceBean(getBaseService());
        return wsFactory.create();
    }

    @Bean(name = "RestWS") @DependsOn( "cxf" )
    public Server getServerRest()
    {
        JAXRSServerFactoryBean restFactory = new JAXRSServerFactoryBean();
        ArrayList<Object> objectArrayList = new ArrayList<>();
        restFactory.setServiceBean(getBaseService());
        restFactory.setProvider(new JacksonJsonProvider());

        return restFactory.create();
    }

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("facade.xml");

    @Bean
    public BaseService getBaseService()
    {
        return (BaseService)applicationContext.getBean("base_service");
    }
}

and finally facade.xml is :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="base_service" class="BaseService"/>

</beans>

When I run my applicatiopn I get NullPointerException :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'RestWS' defined in CxfConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.cxf.endpoint.Server]: Factory method 'getServerRest' threw exception; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098)

The bottom line is that I would like to have both forms of service together and simultaneously but I get the above exception. When I comment one of the getServer or getServerRest method, everything will be OK.


Solution

  • When you want to represent a service in both of SOAP and REST standard you have to define a server for REST and an endpoint for SOAP. For your question, you have to change CxfConfiguration class by the following code:

    import org.apache.cxf.bus.spring.SpringBus;
    import org.apache.cxf.endpoint.Server;
    import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.DependsOn;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import java.util.ArrayList;
    
    @Configuration
    public class CxfConfiguration 
    {
        @Autowired
        private ApplicationContext applicationContext;
    
        @Bean( destroyMethod = "shutdown" )
        public SpringBus cxf() {
            return new SpringBus();
        }
    
        @Bean(name = "RestWS") 
        public Server getServerRest()
        {
            JAXRSServerFactoryBean restFactory = new JAXRSServerFactoryBean();
            ArrayList<Object> objectArrayList = new ArrayList<>();
            restFactory.setServiceBean(getBaseService());
            restFactory.setProvider(new JacksonJsonProvider());
    
            return restFactory.create();
        }
       @Bean 
        public Server getServer() 
        {
            Bus bus = (Bus) applicationContext.getBean(Bus.DEFAULT_BUS_ID);
            Object implementor = getBaseService();
            EndpointImpl endpoint = new EndpointImpl(bus, implementor);
            endpoint.publish("/Base");
            return endpoint ;
        }
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("facade.xml");
    
        @Bean
        public BaseService getBaseService()
        {
            return (BaseService)applicationContext.getBean("base_service");
        }
    }
    

    That can solve your problem but I believe that it's better you have a difference paths or addresses for each REST and SOAP service.