同一项目中的JaxWS, JaxRS Apache CXF + Springboot SOAP和restful服务.&l



我正在将一个应用程序从Spring迁移到Springboot。该应用程序有一堆用Jaxws和Jaxrs编写的SOAP和REST服务。我使用CXF迁移了SOAP服务,并希望将CXF也用于RESTful。但是,一旦我注册了Rest服务器,我的应用程序就开始崩溃了。

这是我使用的两个依赖项

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.4.5</version>
</dependency>

My Soap Service:

package com.example.soap;
import com.example.domain.Student;
import org.springframework.stereotype.Service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlElement;
@Service
@WebService(serviceName = "/MySuperSoapAPI", targetNamespace = "http://mysuperapp.com")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE,use = SOAPBinding.Use.LITERAL)
public class MySuperSoapService {
@WebMethod(operationName = "GetStudent")
@WebResult(name = "Student")
public Student getStudent(@XmlElement(required = true) @WebParam(name = "Student") Student student){
return student;
}
}

My rest式服务

package com.example.rest;
import com.example.domain.Student;
import org.springframework.stereotype.Service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Service
@Path("/student")
public class MyRestService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Student getStudent() {
return new Student("Saifur", "Rahman", "061240");
}
}

配置

package com.example.config;
import com.example.rest.MyRestService;
import com.example.soap.MySuperSoapService;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
import java.util.Arrays;
@Configuration
public class WebConfiguration {
@Autowired
private SpringBus bus;
@Autowired
private MySuperSoapService mySuperSoapService;
@Autowired
private MyRestService myRestService;
@Bean
public Endpoint getStudentEndPoint(){
EndpointImpl endpoint=new EndpointImpl(bus,mySuperSoapService);
endpoint.publish("/student");
return endpoint;
}
@Bean
public Server rsServer() {
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
endpoint.setBus(bus);
endpoint.setAddress("/");
endpoint.setServiceBeans(Arrays.asList(myRestService));
endpoint.setProvider(new JacksonJaxbJsonProvider());
return endpoint.create();
}
}
属性>
cxf.path=/service
#cxf.jaxrs.component-scan=true
#cxf.jaxrs.classes-scan-packages=com.example.rest

我的问题是:

  1. 我可以使用Apache CXF为RS和WS在同一个项目?那么,如何?
  2. 如果更容易的话,我也可以使用spring引导RESTful。但是,似乎也无法配置以使其工作。

任何建议/帮助都是非常感谢的。

完整的项目在Github中发布

是的,您可以同时使用它们。但你不能让他们走同样的路。只需更新下一个路径"/student"保持它们的不同。

@Bean
public Endpoint getStudentEndPoint(){
EndpointImpl endpoint=new EndpointImpl(bus,mySuperSoapService);
endpoint.publish("/student");
return endpoint;
}
@Service
@Path("/student")
public class MyRestService {
...