首页 > 解决方案 > 同一项目中的 JaxWS、JaxRS Apache CXF + Springboot SOAP 和 RESFTful 服务

问题描述

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

这是我正在使用的两个依赖项

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

我的肥皂服务:

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

我的宁静服务

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 boot 用于 RESTful。但是,似乎也无法配置以使其正常工作。

任何建议/帮助表示赞赏。

完整的项目在Github上发布

标签: spring-bootjax-rscxfjax-wsjava-ee-8

解决方案


推荐阅读