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-21 10:01
交口称赞 阅读(6300)
评论(6) 编辑 收藏 所属分类:
Java6