Java6 WebServices (一)服务端

     Java6发布了,其中一个吸引我的新特性就是原生支持WebServices。在这和大家分享下学习心得。
下面就开始写个最简单的WebServices:
package  org.hermit.study.jdk;

import  javax.jws.WebMethod;
import  javax.jws.WebService;
import  javax.jws.soap.SOAPBinding;

@WebService(targetNamespace 
=   " http://jdk.study.hermit.org/client " )
@SOAPBinding(style 
=  SOAPBinding.Style.RPC)
public   class  Hello {
    @WebMethod
    
public  String sayHello(String name) {
        
return   " hello: "   +  name;
    }
}
怎么样简洁吧,很多朋友的写法还要在命令行中执行“ wsgen –cp . <path>
用偶这种方法写的service可以省去上面这步。

targetNamespace = "http://jdk.study.hermit.org/client"这句是指定客户端获取服务端服务后存放的类路径。注意是反着的,http://jdk.study.hermit.org/client在客户端生成的类会放在org.hermit.study.jdk.client包下。

下面是发布服务:
package  org.hermit.study.jdk;

import  javax.xml.ws.Endpoint;

public   class  StartService  {
    
public static void main(String[] args) {
        Endpoint.publish(
"http://localhost:8080/HelloService"new Hello());
    }

}
呵呵,更简洁。一句话而已。
http://localhost:8080/HelloService是指发布的地址

运行
StartService  ,开发浏览器输入:http://localhost:8080/HelloService?wsdl


晕,怎么不能上传图片了。。。。。。。。。。。。
如果能看到以下内容,就可以
  <? xml version="1.0" encoding="UTF-8"  ?>  
< definitions  xmlns ="http://schemas.xmlsoap.org/wsdl/"  xmlns:tns ="http://jdk.study.hermit.org/client"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/"  targetNamespace ="http://jdk.study.hermit.org/client"  name ="HelloService" >
  
< types  />  
< message  name ="sayHello" >
  
< part  name ="arg0"  type ="xsd:string"   />  
  
</ message >
< message  name ="sayHelloResponse" >
  
< part  name ="return"  type ="xsd:string"   />  
  
</ message >
< portType  name ="Hello" >
< operation  name ="sayHello"  parameterOrder ="arg0" >
  
< input  message ="tns:sayHello"   />  
  
< output  message ="tns:sayHelloResponse"   />  
  
</ operation >
  
</ portType >
< binding  name ="HelloPortBinding"  type ="tns:Hello" >
  
< soap:binding  style ="rpc"  transport ="http://schemas.xmlsoap.org/soap/http"   />  
< operation  name ="sayHello" >
  
< soap:operation  soapAction =""   />  
< input >
  
< soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
  
</ input >
< output >
  
< soap:body  use ="literal"  namespace ="http://jdk.study.hermit.org/client"   />  
  
</ output >
  
</ operation >
  
</ binding >
< service  name ="HelloService" >
< port  name ="HelloPort"  binding ="tns:HelloPortBinding" >
  
< soap:address  location ="http://localhost:8080/HelloService"   />  
  
</ port >
  
</ service >
  
</ definitions >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值