springboot整合webservice採用CXF技術
阿新 • • 發佈:2019-02-20
第一步新建一個springboot專案
匯入關於XCF的包,程式碼如下:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<?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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意:包衝突問題,我前面用的是3.1.7版本的包,跟我匯入springboot包發生衝突了,所以換成最新的版本了
如圖:
服務介面:
package com.example.demo.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; /** * WebService介面 * @作者 Administrator * @建立日期 2018/6/23 0023 * @建立時間 11:21. */ @WebService(name = "CommonService", // 暴露服務名稱 targetNamespace = "http://www.WebService.demo.example.com") //名稱空間,一般是介面的包名倒序 public interface CommonService { @WebMethod @WebResult(name = "String",targetNamespace = "") public String HelloWorld(@WebParam(name = "HelloName") String name); }
介面實現:
package com.example.demo.WebService; import org.springframework.stereotype.Component; import javax.jws.WebService; /**介面實現 * @作者 Administrator * @建立日期 2018/6/23 0023 * @建立時間 11:26. */ @WebService(serviceName = "CommonService",//與前面介面一致 targetNamespace = "http://www.WebService.demo.example.com", //與前面介面一致 endpointInterface = "com.example.demo.WebService.CommonService") //介面地址 @Component public class CommonServiceImpl implements CommonService { @Override public String HelloWorld(String name) { return "Hello World!!! --->"+name; } }
配置cxf服務釋出,預設服務在Host:port/services/***路徑下:
package com.example.demo.config;
import com.example.demo.WebService.CommonService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import javax.xml.ws.Endpoint;
/**
* @作者 Administrator
* @建立日期 2018/6/23 0023
* @建立時間 11:31.
*/
@Configuration
public class WebConfig {
@Autowired
private Bus bus;
@Autowired
CommonService service;
/*jax-ws*/
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, service);
endpoint.publish("/CommonService");
return endpoint;
}
}
這裡相當於把Commonservice介面釋出在了路徑/services/CommonService下,wsdl文件路徑為
http://localhost:8080/services/CommonService?wsdl
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();
}
}
}