风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

Java6 WebServices (一)服务端 (ZT)

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>

posted on 2006-12-22 12:53 风人园 阅读(386) 评论(0)  编辑  收藏 所属分类: JavaWeb Service