1. 程式人生 > >spring整和CXF遠端呼叫webservice服務

spring整和CXF遠端呼叫webservice服務

1.在專案中引入CXF的依賴

 <!-- apache-CXF-frontend -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${apache-cxf.frontend.version}</version>
        </dependency>
        <!-- apache-cxf-transports -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${apache-cxf-transports.version}</version>
        </dependency>

2.使用wsimport -s . http://localhost/8081/service/customerService?wsdl解析wsdl檔案生成原生代碼,只需要介面檔案和實體類

3.在spring的配置檔案中註冊cxf的客戶端代理物件

<!--註冊CXF的客戶端代理-->
	<jaxws:client id="crmClient"
				  serviceClass="com.jujung.bos.webservice.CustomerService"
				  address="http://localhost:8081/service/customerService"/>

4.測試程式碼 

/**
 * @author Jujung_Zheng
 * @create 2019-01-04 12:21
 */
public class CXFTest {

    @Test
    public void TestCXFCall() throws Exception {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:spring/applicationContext-service.xml");
        CustomerService bean = context.getBean(CustomerService.class);
        List<Customer> customerList = bean.getCustomerList();
    }
}

測試結果

成功呼叫並返回結果