jdk1.6对webservice的支持示例 .

首先你的JDK为1.6版本以上

1.写一个HelloService.java的类文件代码如下(这里放到了E:/wsclient目录下):

 

Helloservice代码 复制代码
  1. import javax.xml.ws.*;   
  2. import javax.jws.*;   
  3. import javax.jws.soap.*;   
  4. @WebService(targetNamespace="http://localhost:7070/Ebay")   
  5. @SOAPBinding(style=SOAPBinding.Style.RPC)   
  6.   
  7. public class HelloService{   
  8.   
  9.     public static void main(String [] args){   
  10.         Endpoint.publish("http://localhost:7070/Ebay", new HelloService());   
  11.     }   
  12.   
  13.     @WebMethod   
  14.     public void sayHello(){   
  15.         System.out.println("hello");   
  16.     }   
  17. }  
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 ">
  < types />
  < 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目录下),代码如下:

 

Main.java代码 复制代码
  1. package localhost._7070.ebay;   
  2. public class Main{   
  3.     public static void main(String [] args){   
  4.        
  5.         HelloServiceService hss = new HelloServiceService();   
  6.         HelloService hs = hss.getHelloServicePort();   
  7.         hs.sayHello();   
  8.     }   
  9. }  
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已经在服务端打印出来了。到此结束!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值