栏目:Java8基础 作者:admin 日期:2015-04-02 评论:0 点击: 6,297 次
java开发web service最简单的方式是用jdk6自带的支持web service的注解功能。
1、编写代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package net.swiftlet; import javax.jws.WebService; import javax.xml.ws.Endpoint; //targetNamespace的默认值是service类所在包名,也可以设置成发布service的网站域名 @WebService(serviceName = "HelloService", targetNamespace = "www.swiftlet.net") public class HelloService { public String sayhello(String name) { return "Hello " + name + " !"; } public static void main(String[] args) { //发布web service,参数1是服务公开的访问地址address,参数2是服务的具体实现 Endpoint.publish("http://localhost:8080/hello", new HelloService()); } } |
2、运行代码,一般以java application运行方式即可。可以看到运行后进程和启ServerSocket服务程序一样一直出于开启状态。输出信息如下:
2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class net.swiftlet.jaxws.Sayhello
2015-4-2 20:05:16 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class net.swiftlet.jaxws.SayhelloResponse
3、在host系统文件里添加"127.0.0.1 www.swiftlet.net"。打开浏览器,输入"http://www.swiftlet.net:8080/hello",回车,此时页面可能是
Web Services No JAX-WS context information available.
因为一般浏览器不能直接访问WS程序,不过所有的WS都可以生成WSDL文档供一般浏览器访问,方式是在地址后面加上“?wsdl”就可以了,http://www.swiftlet.net:8080/hello?wsdl。
生成的文档如下:
<definitions targetNamespace="www.swiftlet.net" name="HelloService">
<types><xsd:schema><xsd:import namespace="www.swiftlet.net" schemaLocation="http://www.swiftlet.net:8080/hello?xsd=1"/></xsd:schema></types>
<message name="sayhello"><part name="parameters" element="tns:sayhello"/></message>
<message name="sayhelloResponse"><part name="parameters" element="tns:sayhelloResponse"/></message>
<portType name="HelloService">
<operation name="sayhello">
<input message="tns:sayhello"/>
<output message="tns:sayhelloResponse"/>
</operation></portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="sayhello">
<soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation>
</binding>
<service name="HelloService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding"><soap:address location="http://www.swiftlet.net:8080/hello"/>
</port>
</service>
</definitions>
4 除了可以在浏览器上访问发布的service程序,还可以通过客户端来获取。如下代码:
1 2 3 4 5 6 7 8 9 |
package net.swiftlet; public class WebServiceClient { public static void main(String[] args) { HelloService myWebService = new HelloService(); System.out.println(myWebService.sayhello("Swiftlet")); } } |
输出结果为:
Hello Swiftlet !
5 最后想说一点:HelloService类的@WebService注解部分可以简化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@WebService() public class HelloService { public String sayhello(String name) { return "Hello " + name + " !"; } public static void main(String[] args) { //发布web service,参数1是服务公开的访问地址address,参数2是服务的具体实现 Endpoint.publish("http://localhost:8080/hello", new HelloService()); } } |
这样就不用配置host:"127.0.0.1 www.swiftlet.net",可以使用此链接直接在浏览器进行访问:http://localhost:8080/hello?wsdl
------====== 本站公告 ======------
金丝燕网,一个严谨的网站!