一、发布soap1.1版本的协议
添加依赖文件,这个文件夹下的所有文件,然后add build path
验证
二、发布soap1.2版本的协议
2.1、创建接口,并声明为soap1.2
2.2、接口的实现
2.3、发布服务
2.4、验证服务发布是否成功
三、客户端调用cxf发布的服务
注意:Java jdk只支持1.1版本的调用
3.1、生成Java文件方式调用
3.1.1、生成Java文件
使用的两种方式:
wsdl2java -d http://localhost:9999/UserService?wsdl
wsdl2java -d . -p com.dgut.soap12 http://localhost:9999/UserService?wsdl
3.1.2、验证
3.2、使用cxf提供的类调用服务端发布的服务
3.2.1、添加jar包
3.2.2、调用1.1
3.2.3、调用1.2
四、使用soap1.1和soap1.2的区别——服务端接受的数据格式不同
4.1、soap1.1
4.2、soap1.2
所以在进行服务端开发时,应该同时发布1.1和1.2版本,方便选择调用。
4.3、soap1.2、调用天气预报例子
参考来源
代码实现
package com.dgut.client;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class WeatherSoap12 {
public static void main(String[] args) throws Exception {
System.out.println(getWeather("深圳"));
}
public static String getWeather(String city) throws Exception {
// 请求webservice的访问路径
URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
// 打开连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// 服务端接受POST请求
urlConnection.setRequestMethod("POST");
// 向服务端发送xml格式数据
urlConnection.setDoOutput(true);
// 接受服务端数据
urlConnection.setDoInput(true);
// 服务端接受的是xml格式的文件
urlConnection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
// 通过流来写数据到服务端
OutputStream outputStream = urlConnection.getOutputStream();
// 实例化一个buffer
StringBuffer buffer = new StringBuffer();
// 添加
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
buffer.append(
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
buffer.append("<soap12:Body>");
buffer.append("<getWeather xmlns=\"http://WebXml.com.cn/\">");
buffer.append("<theCityCode>").append(city).append("</theCityCode>");
buffer.append("<theUserID></theUserID>");
buffer.append("</getWeather>");
buffer.append("</soap12:Body>");
buffer.append("</soap12:Envelope>");
// 向服务端写数据
outputStream.write(buffer.toString().getBytes());
// 接受服务端传回的数据
InputStream inputStream = urlConnection.getInputStream();
byte[] b = new byte[1024];
int length = 0;
StringBuffer sb = new StringBuffer();
while ((length = inputStream.read(b)) != -1) {// 等于-1说明已经读完
String s = new String(b, 0, length);
sb.append(s);
}
System.out.println(sb.toString());
return sb.toString();
}
}
确认数据集为utf-8
结果