一、什么是webservice
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
二、webservice服务端
可以将注解写到接口当中,本例接口无注解
@WebService(serviceName = "weather", // 与接口中指定的name一致
targetNamespace = "http://cn.gc.service", // 与接口中的命名空间一致,一般是接口的包名倒写
endpointInterface = "com.zhongbaowd.services.entrance.control.webservice.WeatherInterface"// 接口地址
)
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;
/**
* 例:天气查询
* 通过注解可自定义如方法名,参数名等信息
*/
@WebService(
targetNamespace="http://cn.gc.service", //命名空间
name="WeatherWSSoap",
portName="WeatherWSSoapPort",
serviceName="WeatherWS" //该webservice服务的名称
)//@WebService表示该类是一个服务类,需要发布其中的public的方法
//@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//标识使用soap1.1 还是1.2协议通信,默认1.1
public class WeatherInterfaceImpl implements WeatherInterface {
@WebMethod(
operationName="queryWeather", //发布的方法名称
exclude=false // 默认fase: 表示发布该方法 true:表示不发布此方法
)
//WebResult :返回值名称 WebParam:参数名称
@Override
public @WebResult(name="result")String queryWeather(@WebParam(name="cityName")String cityName) {
return "天气良好";
}
}
三、springboot cxf 配置webservice 发布
1.导入必要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.配置发布
- 默认服务发布在Host:port/services/*路径下 ,如需改变默认路径可配置ServletRegistrationBean
- endpoint.publish("/weather"); 发布的路径,此时访问路径为:localhost:20006/ws-api/weather?wsdl
- 多个发布需多个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;
}
四、通过浏览器访问发布的webservice服务wsdl
image.png访问http://localhost:20006/ws-api/weather?wsdl
显示出wsdl说明发布成功
五、生成客户端代码
可利用jdk bin目录下有的wsimport.exe程序生成客户端代码
命令参数
-keep:是否生成java源文件
-d:指定.class文件的输出目录
-s:指定.java文件的输出目录
-p:定义生成类的包名,不定义的话有默认包名
-verbose:在控制台显示输出信息
-b:指定jaxws/jaxb绑定文件或额外的schemas
-extension:使用扩展来支持SOAP1.2
image.png-s 指向项目java目录 -p 包名一定要写,否则默认服务端的类包名,生成之后会报错
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
优点:调用方便
弊端:生成的代码中包含了服务端的wsdl地址,在服务端有ip更改时可直接更改。但是如果方法名等更改了需要重新生成代码
六、客户端调用
image.png1.在wsdl的最下方就可以看出客户端如何调用
2.如需设置超时时间等,需要用到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);// 连接超时时间
policy.setReceiveTimeout(300 * 1000L);// 请求超时时间
conduit.setClient(policy);
return weatherWSSoapPort.queryWeather(cityName);
}
public static void main(String[] args) {
String s = queryWeather("成都");
System.out.println(s);
}
}
image.png
七、HttpClient作为客户端调用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作为客户端调用
优点:工具类复用,调用方便,测试接口时方便,可快速更换接口地址,而不需要重新生成代码,而且可直接控制请求超时时间等 (推荐这种方式)
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;// 请求超时时间
static int connectTimeout = 30000;// 传输超时时间
static Logger logger = Logger.getLogger(HttpClientSoapUtil.class);
/**
* 使用SOAP1.1发送消息,可调1.1,也可调用1.2
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap1_1(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
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) {
// 打印响应内容
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发送消息,只能调用1.2
*
* @param postUrl
* @param soapXml
* @param soapAction
* @return
*/
public static String doPostSoap1_2(String postUrl, String soapXml,
String soapAction) {
String retStr = "";
// 创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(postUrl);
// 设置请求和传输超时时间
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) {
// 打印响应内容
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调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
doPostSoap1_1(postUrl, orderSoapXml, "");
doPostSoap1_1(postUrl, querySoapXml, "");
//采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
//doPostSoap1_2(postUrl, orderSoapXml, "order");
//doPostSoap1_2(postUrl, querySoapXml, "query");
}
}
网友评论