簡體   English   中英

Spring和CXF同時公開一個端點的休息和肥皂

[英]Expose Rest and Soap for one endpoint simultaneously by Spring and CXF

我有一個非常簡單的服務,必須以兩種方式公開:REST和SOAP我編寫了以下代碼:

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

那是我的端點,我將其配置如下:

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");
    }
}

最后facade.xml是:

<?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>

運行我的應用程序時,出現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)

最重要的是,我希望同時提供兩種服務形式,但我得到了上述例外。 當我評論getServergetServerRest方法之一時,一切都會好的。

當您要同時用SOAPREST標准表示服務時,必須為REST定義服務器,並為SOAP定義端點。 對於您的問題,您必須通過以下代碼更改CxfConfiguration類:

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");
    }
}

那可以解決您的問題,但是我認為最好為每個RESTSOAP服務使用不同的路徑或地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM