love fish大鹏一曰同风起,扶摇直上九万里

常用链接

统计

积分与排名

friends

link

最新评论

学习Java6(一) WebServices (1)服务端 (转)

 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 2007-01-12 15:16 liaojiyong 阅读(471) 评论(2)  编辑  收藏 所属分类: WebService

评论

# re: 学习Java6(一) WebServices (1)服务端 2007-04-13 09:19 交口称赞

老大你转载也不著名!!!!!!!!!!!!!!!  回复  更多评论   

# re: 学习Java6(一) WebServices (1)服务端 2007-04-16 09:23 liaojiyong

@交口称赞
写漏了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: