Webservice实现
jax-ws介绍
JAX-WS 的全称为 Java API for XML-Based Webservices ,从java5开始支持JAX-WS2.0版本,Jdk1.6.0_13以后的版本支持2.1版本,jdk1.7支持2.2版本,本教程我们使用jdk1.6.0_45开发。
第一个例子
第一步:服务端开发
1. 编写SEI(Service Endpoint Interface),SEI在webservice中称为port,在java中称为接口,接口类型叫portType。
代码如下:
/**
* SEI天气查询服务接口
* @version V1.0
*/
public interface WeatherInterface {
//天气查询
public String queryWeather(String cityName);
}
2. 编写SEI实现类,此类作为webservice提供服务类
代码如下:
/**
* 天气查询服务
* @version V1.0
*/
@WebService
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String cityName) {
//接收客户端发送过来的数据
System.out.println("from client..."+cityName);
String result = "晴转阴";
//向客户端返回天气查询结果
return result;
}
}
注意:
SEI实现类中至少要有一个非静态的公开方法需要作为webservice服务方法。
public class 上边要加上@WebService
3. endpoint发布服务
public class Server {
public static void main(String[] args) {
//发送天气查询服务
Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
System.out.println("发布webservice服务");
}
}
第二步:客户端开发
根据
wsdl
文档生成客户端调用代码的工具,命令如下
wsimport -d . http://127.0.0.1:12345/weather?wsdl
第三步:客户端编写
代码如下:
/**
* webservice客户端
* @version V1.0
*/
public class Client {
public static void main(String[] args) {
//创建webservice的服务视图
WeatherInterfaceImplService weatherInterfaceImplService =new WeatherInterfaceImplService();
//通过视图得到服务端点
WeatherInterfaceImpl weatherInterfaceImpl= weatherInterfaceImplService.getWeatherInterfaceImplPort();
//可以通过服务端点调用webservice接口
//向服务端发送数据北京
String result = weatherInterfaceImpl.queryWeather("北京");
System.out.println("from server.."+result);
}
}
public class Client2 {
public static void main(String[] args) throws Exception {
//定义url,参数为wsdl地址
URL url = new URL("http://127.0.0.1:54321/weather?wsdl");
//定义qname,第一个参数是命名空间,第二个参数名称是wsdl里边的服务名
QName qName = new QName("http://server.jaxws.webservice.itcast.cn/", "WeatherInterfaceImplService");
//创建服务视图
Service service =Service.create(url, qName);
//得到服务端点
WeatherInterfaceImpl weatherInterfaceImpl= service.getPort(WeatherInterfaceImpl.class);
//通过服务端点调用服务方法
String result = weatherInterfaceImpl.queryWeather("郑州");
System.out.println(result);
}
}
/**
* 使用http+xml请求webservice,测试soap协议内容
* @version V1.0
*/
public class SoapTest {
//测试soap协议
@Test
public void queryWeatherTest() throws Exception{
String urlString = "http://127.0.0.1:12345/weather";
//得到请求的soap协议体
String xml = sendContent();
System.out.println("request:"+xml);
//定义请求url
URL url = new URL(urlString);
//获取请求的链接
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//
httpConn.setRequestProperty("Content-Length", String.valueOf(xml.length()));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//设置http请求的方法为post
httpConn.setRequestMethod("POST");
//需要输出数据向webservice服务端发送数据
httpConn.setDoOutput(true);
//通过输入流得到服务端响应的数据
httpConn.setDoInput(true);
//创建一个输出流
OutputStream out = httpConn.getOutputStream();
//向服务端发送数据
out.write(xml.getBytes());
out.close();
//通过输入流读取服务端响应的内容
byte[] datas=readInputStream(httpConn.getInputStream());
//得到响应的内容
String result=new String(datas);
//返回结果
System.out.println("response:" + result);
}
@Test
public void queryWeathersoap12Test() throws Exception{
String urlString = "http://127.0.0.1:12345/weather";
//得到请求的soap协议体
String xml = sendContent12();
System.out.println("request:"+xml);
//定义请求url
URL url = new URL(urlString);
//获取请求的链接
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//
httpConn.setRequestProperty("Content-Length", String.valueOf(xml.length()));
httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
//设置http请求的方法为post
httpConn.setRequestMethod("POST");
//需要输出数据向webservice服务端发送数据
httpConn.setDoOutput(true);
//通过输入流得到服务端响应的数据
httpConn.setDoInput(true);
//创建一个输出流
OutputStream out = httpConn.getOutputStream();
//向服务端发送数据
out.write(xml.getBytes());
out.close();
//通过输入流读取服务端响应的内容
byte[] datas=readInputStream(httpConn.getInputStream());
//得到响应的内容
String result=new String(datas);
//返回结果
System.out.println("response:" + result);
}
/**
* 从输入流中读取数据
*
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();// 网页的二进制数据
outStream.close();
inStream.close();
return data;
}
//请求的soap协议体
public String sendContent(){
String content="<?xml version=\"1.0\" ?>"
+ "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<S:Body>"
+"<queryWeather xmlns=\"http://server.jaxws.webservice.itcast.cn/\">"
+"<arg0>郑州</arg0>"
+"</queryWeather>"
+"</S:Body>"
+"</S:Envelope>";
return content;
}
//请求的soap1.2协议体
public String sendContent12(){
String content="<?xml version=\"1.0\" ?>"
+ "<S:Envelope xmlns:S=\"http://www.w3.org/2003/05/soap-envelope\">"
+ "<S:Body>"
+"<queryWeather xmlns=\"http://server.jaxws.webservice.itcast.cn/\">"
+"<arg0>郑州</arg0>"
+"</queryWeather>"
+"</S:Body>"
+"</S:Envelope>";
return content;
}
}