一,服务端
</pre><pre name="code" class="java">package webservice;
import pool.ThreadPool;
import javax.xml.ws.Endpoint;
/**
* Created by Juxuny on 2014/9/24.
*/
public class Main {
public static void main(String[] args) {
System.out.println("start service.");
Endpoint.publish("http://localhost:8888/hello", new MyHello());
}
}
package webservice;
import javax.jws.WebService;
/**
* Created by Juxuny on 2014/9/24.
*/
@WebService(endpointInterface = "webservice.HelloWorld")
public class MyHello implements HelloWorld {
/**
*
* @param x 加数
* @param y 被加数
* @return 结果
*/
@Override
public String add(int x, int y) {
System.out.println("x + y = ?");
return x + y + "";
}
}
package webservice;
import javax.jws.WebService;
/**
* Created by Juxuny on 2014/9/24.
*/
@WebService
public interface HelloWorld {
public String add(int x ,int y);
}
运行后,在浏览器打开下面的URL,能够看到一个 xml文件就表示成功了
二、客户端
使用wsimport 指令创建客户端
wsimport -p hello -keep http://localhost:8888/hello?wsdl
运行完之后会见到一个目录hello
还可以创建成jar包
wsimport -p hello -keep http://localhost:8888/hello?wsdl -clientjar hello.jar
将这个jar添加到自己的工程之后,就可以调用WebService
package test;
import hello.HelloWorld;
import hello.MyHelloService;
import hello.ObjectFactory;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Juxuny on 2014/9/24.
*/
public class Main {
public static void main(String[] args) throws MalformedURLException {
MyHelloService myHelloService = new MyHelloService(new URL("http://localhost:8888/hello"));
HelloWorld helloWorld = myHelloService.getMyHelloPort();
System.out.println(helloWorld.add(1,3));
}
}