服务发布端:
MyServer:
package org.zttc.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());
}
}
IMyService:
package org.zttc.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="loginUser")
public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);
}
MyServiceImpl:
package org.zttc.service;
import javax.jws.WebParam;
import javax.jws.WebService;
//指定实现的接口
@WebService(endpointInterface="org.zttc.service.IMyService")
public class MyServiceImpl implements IMyService {
public int add(int a, int b) {
// TODO Auto-generated method stub
System.out.println(a+"+"+b+"="+(a+b));
return (a+b);
}
public int minus(int a, int b) {
// TODO Auto-generated method stub
System.out.println(a+"-"+b+"="+(a-b));
return (a-b);
}
public User login(String username,String password){
System.out.println(username+"is logining!");
User user=new User();
user.setId(1);
user.setUsername(username);
user.setPasswd(password);
return user;
}
}
====================================================================================================================================
客户端:先使用wsimport命令把接口等导入到本机
TestClient:
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) throws MalformedURLException{
//创建访问wsdl服务地址的url
URL url=new URL("http://localhost:6666/ns?wsdl");
//通过Qname指明服务的具体信息
QName sname=new QName("http://service.zttc.org/","MyServiceImplService");
//创建服务
Service service=Service.create(url, sname);
//实现接口
IMyService ms=service.getPort(IMyService.class);
User user=ms.login("hanyijun", "123456");
System.out.println(user.getId());
//以上服务有问题,依然依赖于IMyService接口
}
}