1简历webservice服务的步骤:
(1)服务器的建立:1:创建接口SEI(Service Endpoint Interface)
@WebService()
public interface IMyService
{
@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);
@WebResult(name="loginUser")
public User login(@WebParam(name="username")String
username,@WebParam(name="password")String password);
}
2:创建实现类SIB(Service inplemention Bean)
@WebService(endpointInterface="org.zttc.service.IMyService")
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;
}
@Override
public User login(String username, String password) {
System.out.println(username+" is logining");
User user = new User();
user.setId(1);
user.setUsername(username);
user.setPassword(password);
return user;
}
}
3:开启服务
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,看到页面上出现xml文件。
(2)客户端的建立:
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));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
3.wsdl的有关参数:
types:用来定义访问的类型
message:SOAP
portType:指明服务器的接口,并且通过operation绑定相应的in和out的消息:其中in表示参数,out表示返回值
binding:指定传递消息所使用的格式
service:指定服务所发布的名称
4:dtd的例子:
classroom.dtd
<?xml version="1.0"
encoding="UTF-8"?>
<!ELEMENT classroom
(claName,grade,students)>
<!ATTLIST classroom id ID #REQUIRED>
<!ELEMENT claName (#PCDATA)>
<!ELEMENT grade (#PCDATA)>
<!ELEMENT students (student+)>
<!ELEMENT student (id,stuName,age)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT stuName (#PCDATA)>
<!ELEMENT age (#PCDATA)>
classroom.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!DOCTYPE classroom SYSTEM
"classroom.dtd">
<classroom id="c1">
<claName>10计算机应用技术</claName>
<grade>2010</grade>
<students>
<student>
<id>1</id>
<stuName>zhangsan</stuName>
<age>12</age>
</student>
<student>
<id>2</id>
<stuName>lisi</stuName>
<age>122</age>
</student>
</students>
</classroom>