一 服务器建立
1.1 创建接口
Web服务的接口通常称为SEI (Service Endpoint Interface)。
package org.zttc.service;
import javax.jws.WebService;
@WebService()
public interface IMyService {
public int add(int a,int b);
public int minus(int a,int b);
}
1.2 创建实现类
Web服务的实现类通常称为SIB (Service Implementation Bean)
package org.zttc.service;
import javax.jws.WebService;
//@WebService(endpointInterface="org.zttc.service.IMyService")// 用于JDK6.0以上的
@WebService(serviceName="MyServiceImpl")
public class MyServiceImpl implements IMyService {
@Override
public int add(int a,int b) {
System.out.println(a+"+"+b+"="+(a+b));
return a+b;
}
@Override
public int minus(int a,int b) {
System.out.println(a+"-"+b+"="+(a-b));
return a-b;
}
}
1.3 创建服务
package org.zttc.service;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:8888/ns";
Endpoint.publish(address,new MyServiceImpl());
}
}
运行本类,在浏览器中敲入 http://localhost:8888/ns?wsdl 可以查看wsdl文件
二 客户端的使用
package org.zttc.service;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class TestClient {
public static void main(String[] args) {
try {
//创建访问wsdl服务地址的url
URL url = new URL("http://localhost:8888/ns?wsdl");
//通过Qname指明服务的具体信息
QName sname = new QName("http://service.zttc.org/","MyServiceImplService");
//创建服务
Service service = Service.create(url,sname);
//实现接口
IMyService ms = service.getPort(IMyService.class);
System.out.println(ms.add(12,33));
//以上服务有问题,依然依赖于IMyServie接口
} catch (MalformedURLException e) {
e.printstacktrace();
}
}
}
QName 的创建参数来自WSDL文件
<deFinitions targetNamespace="http://service.zttc.org/" name="MyServiceImplService">
三 wsimport的使用
cmd> wsimport -s 保存路径 http://localhost:9999/ns?wsdl
cmd> wsimport -d d:/webservice/01 -keep -verbose http://localhost:8888/ns?wsdl
-d 指定生成的目录
-keep 保持源文件
-p 指定包名
客户端代码
package org.zttc.service;
import java.net.MalformedURLException;
public class TestClient2 {
public static void main(String[] args) throws MalformedURLException {
MyServiceImplService msis = new MyServiceImplService();
IMyService ms = msis.getMyServiceImplPort();
System.out.println(ms.minus(29,11));
}
}
四 wsdl简单讲解
4.1 types
用来定义访问的类型,描述方法名、参数、返回值
http://localhost:8888/ns?xsd=1
4.2 message
SOAP(simple object access protocol)消息,一个方法一般对应两个,接收消息与发送消息。
4.3 portType
指明服务器的接口,并且通过operation绑定相应的in和out的消息:其中in表示参数,out表示返回值
4.4 binding
指定传递消息所使用的格式。早期会用soap encoding ,现在是literal
4.5 service
指定服务所发布的名称等基本信息
五 soap的使用和TCPMon
5.1 在eclipse中可以查看soap消息格式
J2EE视图->Launch the Web Services Explorer->WSDL网址
5.2 TCPMon
Listen Port #
客户端访问的接口,等于客户端首先将消息提交给TCPMon之后,再由TCPMon转发给服务器
Listener
Target Hostname
Target Port #
服务器的地址,TCPMon转发的地址
@WebResult(name="addResult") public int add(@WebParam(name="a")int a,@WebParam(name="b")int b); @WebResult(name="minusResult") public int minus(@WebParam(name="a")int a,@WebParam(name="b")int b);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。