一凫海、什么是webservice
Web Service技術(shù)洽胶, 能使得運(yùn)行在不同機(jī)器上的不同應(yīng)用無(wú)須借助附加的塔橡、專門的第三方軟件或硬件图焰, 就可相互交換數(shù)據(jù)或集成启盛。依據(jù)Web Service規(guī)范實(shí)施的應(yīng)用之間蹦掐, 無(wú)論它們所使用的語(yǔ)言技羔、 平臺(tái)或內(nèi)部協(xié)議是什么, 都可以相互交換數(shù)據(jù)卧抗。Web Service是自描述藤滥、 自包含的可用網(wǎng)絡(luò)模塊, 可以執(zhí)行具體的業(yè)務(wù)功能社裆。Web Service也很容易部署拙绊, 因?yàn)樗鼈兓谝恍┏R?guī)的產(chǎn)業(yè)標(biāo)準(zhǔn)以及已有的一些技術(shù),諸如標(biāo)準(zhǔn)通用標(biāo)記語(yǔ)言下的子集XML、HTTP标沪。Web Service減少了應(yīng)用接口的花費(fèi)榄攀。Web Service為整個(gè)企業(yè)甚至多個(gè)組織之間的業(yè)務(wù)流程的集成提供了一個(gè)通用機(jī)制。
二金句、webservice服務(wù)端
可以將注解寫到接口當(dāng)中檩赢,本例接口無(wú)注解
@WebService(serviceName = "weather", // 與接口中指定的name一致
targetNamespace = "http://cn.gc.service", // 與接口中的命名空間一致,一般是接口的包名倒寫
endpointInterface = "com.zhongbaowd.services.entrance.control.webservice.WeatherInterface"http:// 接口地址
)
package cn.itcast.ws.jaxws.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* 例:天氣查詢
* 通過注解可自定義如方法名,參數(shù)名等信息
*/
@WebService(
targetNamespace="http://cn.gc.service", //命名空間
name="WeatherWSSoap",
portName="WeatherWSSoapPort",
serviceName="WeatherWS" //該webservice服務(wù)的名稱
)//@WebService表示該類是一個(gè)服務(wù)類违寞,需要發(fā)布其中的public的方法
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//標(biāo)識(shí)使用soap1.1 還是1.2協(xié)議通信,默認(rèn)1.1
public class WeatherInterfaceImpl implements WeatherInterface {
@WebMethod(
operationName="queryWeather", //發(fā)布的方法名稱
exclude=false // 默認(rèn)fase: 表示發(fā)布該方法 true:表示不發(fā)布此方法
)
//WebResult :返回值名稱 WebParam:參數(shù)名稱
@Override
public @WebResult(name="result")String queryWeather(@WebParam(name="cityName")String cityName) {
return "天氣良好";
}
}
三贞瞒、springboot cxf 配置webservice 發(fā)布
1.導(dǎo)入必要jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.0</version>
</dependency>
2.配置發(fā)布
- 默認(rèn)服務(wù)發(fā)布在Host:port/services/*路徑下 ,如需改變默認(rèn)路徑可配置ServletRegistrationBean
- endpoint.publish("/weather"); 發(fā)布的路徑趁曼,此時(shí)訪問路徑為:localhost:20006/ws-api/weather?wsdl
- 多個(gè)發(fā)布需多個(gè)Endpoint @Bean
import com.zhongbaowd.services.entrance.control.webservice.EsbWebService;
import com.zhongbaowd.services.entrance.control.webservice.EsbWebServiceImpl;
import com.zhongbaowd.services.entrance.control.webservice.WeatherInterface;
import com.zhongbaowd.services.entrance.control.webservice.WeatherInterfaceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* Created by yanfazhongxin on 2018/10/16.
*/
@Configuration
public class EsbWebServiceConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/ws-api/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public EsbWebService esbWebService() {
return new EsbWebServiceImpl();
}
@Bean
public WeatherInterface weatherInterface() {
return new WeatherInterfaceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), esbWebService());
endpoint.publish("/apis");
return endpoint;
}
@Bean
public Endpoint endpoint2() {
EndpointImpl endpoint = new EndpointImpl(springBus(), weatherInterface());
endpoint.publish("/weather");
return endpoint;
}
四军浆、通過瀏覽器訪問發(fā)布的webservice服務(wù)wsdl
訪問http://localhost:20006/ws-api/weather?wsdl
顯示出wsdl說明發(fā)布成功
五、生成客戶端代碼
可利用jdk bin目錄下有的wsimport.exe程序生成客戶端代碼
命令參數(shù)
-keep:是否生成java源文件
-d:指定.class文件的輸出目錄
-s:指定.java文件的輸出目錄
-p:定義生成類的包名挡闰,不定義的話有默認(rèn)包名
-verbose:在控制臺(tái)顯示輸出信息
-b:指定jaxws/jaxb綁定文件或額外的schemas
-extension:使用擴(kuò)展來支持SOAP1.2
-s 指向項(xiàng)目java目錄 -p 包名一定要寫乒融,否則默認(rèn)服務(wù)端的類包名,生成之后會(huì)報(bào)錯(cuò)
wsimport -keep -s D:\IDESpaceWork\zhongbaowd-services\ws-entrance-control-service\src\main\java -p com.zhongbaowd.services.entrance.control.wsimport.resource.weather http://localhost:20006/ws-api/weather?wsdl
優(yōu)點(diǎn):調(diào)用方便
弊端:生成的代碼中包含了服務(wù)端的wsdl地址摄悯,在服務(wù)端有ip更改時(shí)可直接更改簇抵。但是如果方法名等更改了需要重新生成代碼
六、客戶端調(diào)用
1.在wsdl的最下方就可以看出客戶端如何調(diào)用
2.如需設(shè)置超時(shí)時(shí)間等射众,需要用到cxf的ClientProxy 代理
package com.zhongbaowd.services.entrance.control.wsimport.client;
import com.zhongbaowd.services.entrance.control.wsimport.resource.weather.WeatherWS;
import com.zhongbaowd.services.entrance.control.wsimport.resource.weather.WeatherWSSoap;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
* weather webservice客戶端
* Created by yanfazhongxin on 2018/10/19.
*/
public class WeatherClient {
/**
* 查詢天氣
* @param cityName
* @return
*/
public static String queryWeather(String cityName){
WeatherWS weatherWS = new WeatherWS();
WeatherWSSoap weatherWSSoapPort = weatherWS.getWeatherWSSoapPort();
Client proxy = ClientProxy.getClient(weatherWSSoapPort );
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(300 * 1000L);// 連接超時(shí)時(shí)間
policy.setReceiveTimeout(300 * 1000L);// 請(qǐng)求超時(shí)時(shí)間
conduit.setClient(policy);
return weatherWSSoapPort.queryWeather(cityName);
}
public static void main(String[] args) {
String s = queryWeather("成都");
System.out.println(s);
}
}
七碟摆、HttpClient作為客戶端調(diào)用webservice
首先引入jar包
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
HttpClient作為客戶端調(diào)用
優(yōu)點(diǎn):工具類復(fù)用,調(diào)用方便叨橱,測(cè)試接口時(shí)方便典蜕,可快速更換接口地址,而不需要重新生成代碼罗洗,而且可直接控制請(qǐng)求超時(shí)時(shí)間等 (推薦這種方式)
package com.zhongbaowd.services.entrance.control.httpClient;
import java.nio.charset.Charset;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class HttpClientSoapUtil{
static int socketTimeout = 30000;// 請(qǐng)求超時(shí)時(shí)間
static int connectTimeout = 30000;// 傳輸超時(shí)時(shí)間
static Logger logger = Logger.getLogger(HttpClientSoapUtil.class);
/**
* 使用SOAP1.1發(fā)送消息愉舔,可調(diào)1.1,也可調(diào)用1.2
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap1_1(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 創(chuàng)建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印響應(yīng)內(nèi)容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 釋放資源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doPostSoap1_1", e);
}
return retStr;
}
/**
* 使用SOAP1.2發(fā)送消息,只能調(diào)用1.2
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap1_2(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 創(chuàng)建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout).build();
httpPost.setConfig(requestConfig);
try {
httpPost.setHeader("Content-Type",
"application/soap+xml;charset=UTF-8");
httpPost.setHeader("SOAPAction", soapAction);
StringEntity data = new StringEntity(soapXml,
Charset.forName("UTF-8"));
httpPost.setEntity(data);
CloseableHttpResponse response = closeableHttpClient
.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
// 打印響應(yīng)內(nèi)容
retStr = EntityUtils.toString(httpEntity, "UTF-8");
logger.info("response:" + retStr);
}
// 釋放資源
closeableHttpClient.close();
} catch (Exception e) {
logger.error("exception in doPostSoap1_2", e);
}
return retStr;
}
public static void main(String[] args) {
String orderSoapXml = "<?xml version = \"1.0\" ?>"
+ "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.b.com\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ " <web:order soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ " <in0 xsi:type=\"web:OrderRequest\">"
+ " <mobile xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</mobile>"
+ " <orderStatus xsi:type=\"xsd:int\">?</orderStatus>"
+ " <productCode xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</productCode>"
+ " </in0>" + " </web:order>"
+ " </soapenv:Body>" + "</soapenv:Envelope>";
String querySoapXml = "<?xml version = \"1.0\" ?>"
+ "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservices.b.com\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ " <web:query soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
+ " <in0 xsi:type=\"web:QueryRequest\">"
+ " <endTime xsi:type=\"xsd:dateTime\">?</endTime>"
+ " <mobile xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">?</mobile>"
+ " <startTime xsi:type=\"xsd:dateTime\">?</startTime>"
+ " </in0>" + " </web:query>"
+ " </soapenv:Body>" + "</soapenv:Envelope>";
String postUrl = "http://localhost:8080/services/WebServiceFromB";
//采用SOAP1.1調(diào)用服務(wù)端伙菜,這種方式能調(diào)用服務(wù)端為soap1.1和soap1.2的服務(wù)
doPostSoap1_1(postUrl, orderSoapXml, "");
doPostSoap1_1(postUrl, querySoapXml, "");
//采用SOAP1.2調(diào)用服務(wù)端轩缤,這種方式只能調(diào)用服務(wù)端為soap1.2的服務(wù)
//doPostSoap1_2(postUrl, orderSoapXml, "order");
//doPostSoap1_2(postUrl, querySoapXml, "query");
}
}