webservice基础知识
异构平台间的交互
java实现:
JAX-WS
框架:
CXF
AXIS
METRO
jax方式生成新的接口和实现接口的类(用于存放在client端)
-d 指定生成的目录
-keep 指定是否生成源文件
-p 指定报名
-verbose显示生成详细过程
java:wsimport -d D:\sid\workspace_webservice\wsimport -keep -verbose http://localhost:6666/ns?wsdl
SEI(Service EndPoint Interface)
SIB(Service implement Bean)
WSDL基础:
SOAP:Simple object access Protocol(协议)
SOAPMessage:
WSDL:(url后出现的)
types:用来定义访问的类型
message:SOAP
portType:指明接口名称(绑定in和out;operation:指定方法名称)
binding:指定传递消息所使用的格式(style:document和rpc)
service:指定服务所发布的基本信息
java EE视图:
可以查看web service Explorer
可以看到SOAP协议的发送与返回的xml
可以用tcpMon来捕获更清晰的xml
可以设置自己的监听端口,然后再用tcpmon来进行转发
实例:
package org.sid.service;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@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="login")
public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);
}
package org.sid.service;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(endpointInterface="org.sid.service.IMyService")
public class MyServiceImpl implements IMyService {
@Override
public int add(int a, int b) {
System.out.println(a+b);
return a+b;
}
@Override
public int minus(int a, int b) {
System.out.println(a-b);
return a-b;
}
@Override
@WebResult(name = "login")
public User login(@WebParam(name = "username") String username,
@WebParam(name = "password") String password) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
}
package org.sid.service;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:6666/ns";
Endpoint.publish(address, new MyServiceImpl());
}
}
package org.sid.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:6666/ns?wsdl");
//通过Qname致命服务的具体信息namespaceURI和localPart在http://localhost:6666/ns?wsdl中查找
QName qname = new QName("http://service.sid.org/","MyServiceImplService");
//创建服务
Service service = Service.create(url, qname);
//实现接口(依赖于接口了)
IMyService ms = service.getPort(IMyService.class);
ms.add(23, 21);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
wsimport之后,将生成的文件考入新的client项目中
使用如下客户端调用:
package org.sid.service;
public class TestClient2 {
public static void main(String[] args) {
MyServiceImplService msis = new MyServiceImplService();
IMyService ms = msis.getMyServiceImplPort();
ms.add(12, 2);
}
}