Java6开发WebService

之前常常用CXF、Axis2、XFire等结合Java语言来开发Web Service应用,这样的好处是用途广,灵活,另外一个重要原因是我们的生产环境是Java5。
但实际上Java6中已经支持用Java开发WebService应用了,而且很方便。这样就大大减少了项目安装部署的代价,因为选择开源的框架依赖大量第三方包,程序的尺寸倍增。
下面是一个用Java6开发WebService的例子。
[color=blue]一、web service服务端代码[/color]:
package com.test.wsserver;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class Java6WS {

@WebMethod
public String doSomeThing() {
return "Hello Java6 Web Service";
}

public static void main(String[] args){
Endpoint.publish("http://localhost:8080/sky/java6ws", new Java6WS());
}
}


运行后,控制台的输出为:
2010-12-31 9:28:36 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class com.test.wsserver.jaxws.DoSomeThing
2010-12-31 9:28:36 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class com.test.wsserver.jaxws.DoSomeThingResponse


web service发布对应的wsdl的获取:在浏览器中访问:http://localhost:8080/sky/java6ws?wsdl,respond即为wsdl:
<?xml version="1.0" encoding="UTF-8" ?> 
- <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
- <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://wsserver.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://wsserver.test.com/" name="Java6WSService">
- <types>
- <xsd:schema>
<xsd:import namespace="http://wsserver.test.com/" schemaLocation="http://localhost:8080/sky/java6ws?xsd=1" />
</xsd:schema>
</types>
- <message name="doSomeThing">
<part name="parameters" element="tns:doSomeThing" />
</message>
- <message name="doSomeThingResponse">
<part name="parameters" element="tns:doSomeThingResponse" />
</message>
- <portType name="Java6WS">
- <operation name="doSomeThing">
<input message="tns:doSomeThing" />
<output message="tns:doSomeThingResponse" />
</operation>
</portType>
- <binding name="Java6WSPortBinding" type="tns:Java6WS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="doSomeThing">
<soap:operation soapAction="" />
- <input>
<soap:body use="literal" />
</input>
- <output>
<soap:body use="literal" />
</output>
</operation>
</binding>
- <service name="Java6WSService">
- <port name="Java6WSPort" binding="tns:Java6WSPortBinding">
<soap:address location="http://localhost:8080/sky/java6ws" />
</port>
</service>
</definitions>


由以上内容可以看出在java6中开发web service并发布是很容易的事情。

[color=blue]二、web service客户端代码[/color]
java6提供了根据web service服务端生成客户端代码的工具,生成的时候需要服务端处于运行状态。
[img]http://dl.iteye.com/upload/attachment/379427/d169bb89-08c7-3c9d-b6ef-e58c037d947a.jpg[/img]
命令的格式为:wsimport -p 客户端存放路径 -keep wsdl的响应url

命令执行后,客户端程序将生成到目录com.test.wsclient中
[img]http://dl.iteye.com/upload/attachment/379437/64065059-0d52-3745-91c6-ad1b48906434.jpg[/img]

[color=blue]三、客户端测试代码[/color]
package com.test;

import com.test.wsclient.Java6WS;
import com.test.wsclient.Java6WSService;

public class TestClient {
public static void main(String[] args) {
Java6WSService wsService = new Java6WSService();
Java6WS java6ws = wsService.getJava6WSPort();
String returnContentString = java6ws.doSomeThing();
System.out.println(returnContentString);
}

}

注意:测试类中引用的类都是com.test.wsclient中的。

测试类执行的结果是在控制台输出以下内容:
Hello Java6 Web Service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值