Java6开发WebService

    之前常常用CXF、Axis2、XFire等结合Java语言来开发Web Service应用,这样的好处是用途广,灵活,另外一个重要原因是我们的生产环境是Java5。
    但实际上Java6中已经支持用Java开发WebService应用了,而且很方便。这样就大大减少了项目安装部署的代价,因为选择开源的框架依赖大量第三方包,程序的尺寸倍增。
    下面是一个用Java6开发WebService的例子。
    一、web service服务端代码
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并发布是很容易的事情。

    二、web service客户端代码
    java6提供了根据web service服务端生成客户端代码的工具,生成的时候需要服务端处于运行状态。
Java6开发WebService_第1张图片
    命令的格式为:wsimport -p 客户端存放路径 -keep wsdl的响应url

    命令执行后,客户端程序将生成到目录com.test.wsclient中
Java6开发WebService_第2张图片
   
    三、客户端测试代码
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

你可能感兴趣的:(jdk,xml,应用服务器,Web,webservice)