springboot整合webservice


轉自https://blog.csdn.net/pharaohsprince/article/details/75579630,感謝度娘,感謝博主

spring boot整合cxf發布webservice服務和cxf客戶端調用
本案例使用maven方式
核顯文件清單
1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.leftso</groupId>
    <artifactId>demo-webservice-cxf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>demo-webservice-cxf</name>
    <description>Demo project for Spring Boot security</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>
        <!-- CXF webservice -->
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
 
</project>


2.CommonService.java 服務接口

package com.leftso.webservice;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
 
/**
 * 接口
 * 
 * @author leftso
 *
 */
@WebService(name = "CommonService", // 暴露服務名稱
        targetNamespace = "http://webservice.leftso.com/"// 命名空間,一般是接口的包名倒序
)
public interface CommonService {
    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    public String sayHello(@WebParam(name = "userName") String name);
 
}

3.接口實現

package com.leftso.webservice;
 
import javax.jws.WebService;
 
import org.springframework.stereotype.Component;
 
/**
 * 接口實現
 * 
 * @author leftso
 *
 */
@WebService(serviceName = "CommonService", // 與接口中指定的name一致
        targetNamespace = "http://webservice.leftso.com/", // 與接口中的命名空間一致,一般是接口的包名倒
        endpointInterface = "com.leftso.webservice.CommonService"// 接口地址
)
@Component
public class CommonServiceImp implements CommonService {
 
    @Override
    public String sayHello(String name) {
 
        return "Hello ," + name;
    }
 
}

4.配置cxf服務發布,默認服務在Host:port/services/***路徑下

package com.leftso.config;
 
import javax.xml.ws.Endpoint;
 
import org.apache.cxf.Bus;
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 com.leftso.webservice.CommonService;
 
@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;
 
    @Autowired
    CommonService commonService;
 
    /** JAX-WS **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, commonService);
        endpoint.publish("/CommonService");
        return endpoint;
    }
}

這里相當於把Commonservice接口發布在了路徑/services/CommonService下,wsdl文檔路徑為
http://localhost:8080/services/CommonService?wsdl

創建基於cxf的客服端調用webservice接口(非使用wsdl文檔生成java類)

package com.leftso.client;
 
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
 
import com.leftso.webservice.CommonService;
 
public class CxfClient {
    public static void main(String[] args) {
        cl1();
    }
 
    /**
     * 方式1.代理類工廠的方式,需要拿到對方的接口
     */
    public static void cl1() {
        try {
            // 接口地址
            String address = "http://localhost:8080/services/CommonService?wsdl";
            // 代理工廠
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 設置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 設置接口類型
            jaxWsProxyFactoryBean.setServiceClass(CommonService.class);
            // 創建一個代理接口實現
            CommonService cs = (CommonService) jaxWsProxyFactoryBean.create();
            // 數據准備
            String userName = "Leftso";
            // 調用代理接口的方法調用並返回結果
            String result = cs.sayHello(userName);
            System.out.println("返回結果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 動態調用方式
     */
    public static void cl2() {
        // 創建動態客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/CommonService?wsdl");
        // 需要密碼的情況需要加上用戶名和密碼
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",參數1,參數2,參數3....);
            objects = client.invoke("sayHello", "Leftso");
            System.out.println("返回數據:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}
調用后返回結果輸出為
Hello,Leftso


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM