首先你的JDK为1.6版本以上
1.写一个HelloService.java的类文件代码如下(这里放到了E:/wsclient目录下):
- import javax.xml.ws.*;
- import javax.jws.*;
- import javax.jws.soap.*;
- @WebService(targetNamespace="http://localhost:7070/Ebay")
- @SOAPBinding(style=SOAPBinding.Style.RPC)
-
- public class HelloService{
-
- public static void main(String [] args){
- Endpoint.publish("http://localhost:7070/Ebay", new HelloService());
- }
-
- @WebMethod
- public void sayHello(){
- System.out.println("hello");
- }
- }
import javax.xml.ws.*;
import javax.jws.*;
import javax.jws.soap.*;
@WebService(targetNamespace="http://localhost:7070/Ebay")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class HelloService{
public static void main(String [] args){
Endpoint.publish("http://localhost:7070/Ebay", new HelloService());
}
@WebMethod
public void sayHello(){
System.out.println("hello");
}
}
2.到命令行下进行编译然后运行:
E:/wsclient>javac HelloService.java
E:/wsclient>java HelloService
3.不要结束程序,打开浏览器地址栏中输入:
http://localhost:7070/Ebay?wsdl
回车访问当看到下面的内容时,服务发布成功:
<?xml version="1.0" encoding="UTF-8" ?>
-
<
definitions
xmlns
="
http://schemas.xmlsoap.org/wsdl/
"
xmlns:tns
="
http://localhost:7070/Ebay
"
xmlns:xsd
="
http://www.w3.org/2001/XMLSchema
"
xmlns:soap
="
http://schemas.xmlsoap.org/wsdl/soap/
"
targetNamespace
="
http://localhost:7070/Ebay
"
name
="
HelloServiceService
">
<
message
name
="
sayHello
" />
<
message
name
="
sayHelloResponse
" />
-
<
portType name
="
HelloService
">
-
<
operation name
="
sayHello
"
parameterOrder
="">
<
input
message
="
tns:sayHello
" />
<
output
message
="
tns:sayHelloResponse
" />
</
operation
>
</
portType
>
-
<
binding name
="
HelloServicePortBinding
"
type
="
tns:HelloService
">
<
soap:binding
style
="
rpc
"
transport
="
http://schemas.xmlsoap.org/soap/http
" />
-
<
operation name
="
sayHello
">
<
soap:operation
soapAction
="" />
-
<
input
>
<
soap:body
use
="
literal
"
namespace
="
http://localhost:7070/Ebay
" />
</
input
>
-
<
output
>
<
soap:body
use
="
literal
"
namespace
="
http://localhost:7070/Ebay
" />
</
output
>
</
operation
>
</
binding
>
-
<
service name
="
HelloServiceService
">
-
<
port name
="
HelloServicePort
"
binding
="
tns:HelloServicePortBinding
">
<
soap:address
location
="
http://localhost:7070/Ebay
" />
</
port
>
</
service
>
</
definitions
>
4.不要结束程序运行,再开一个命令窗口生成一个访问服务的客户端,命令窗口如下:
E:/wsclient>wsimport -d e:/wsclient http://localhost:7070/Ebay?wsdl
回车运行此时会在e盘的wsclient目录下生成localhost文件夹:
进去目录结构为E:/wsclient/localhost/_7070/ebay:
在ebay目录下面有两个文件,分别是HelloService.class和HelloServiceService.class。
只是class文件不利于我们学习,我们再生成源代码,运行如下命令:
E:/wsclient>wsimport -s e:/wsclient -d e:/wsclient http://localhost:7070/Ebay?wsdl
回车运行,此时会在ebay目录下生成HelloService.java和HelloServiceService.java
打开看一下会发现HelloService.java是一个接口,要通过客户端调用,我们应该能得到一个代理,
打开HelloServiceService.java看一下,里面有一个getHelloServicePort()的方法返回的就是一个HelloService;
这个时候我们就可以写一个客户端进行调用了.
5.写一个客户端调用的类Main.java(这里文件放在E:/wsclient,然后编译将class生成到ebay目录下),代码如下:
- package localhost._7070.ebay;
- public class Main{
- public static void main(String [] args){
-
- HelloServiceService hss = new HelloServiceService();
- HelloService hs = hss.getHelloServicePort();
- hs.sayHello();
- }
- }
package localhost._7070.ebay;
public class Main{
public static void main(String [] args){
HelloServiceService hss = new HelloServiceService();
HelloService hs = hss.getHelloServicePort();
hs.sayHello();
}
}
运行命令:E:/wsclient>javac -d . Main.java
它会将class文件自动生成到相应的包中
再用如下命令运行程序:
E:/wsclient>java localhost._7070.ebay.Main
回车运行,这个时候在当前命令窗口什么也没看见,因为程序运行在服务端。切换到另一个开启服务的命令窗口:
窗口显示如下:
E:/wsclient>java HelloService
hello
hello已经在服务端打印出来了。到此结束!