Java6开发WebService入门
之前常常用CXF、Axis2、XFire等来开发结合Java语言来开发Web Service应用,这样的好处是用途广,灵活,另外一个重要原因是我们的生产环境是Java5。
但实际上Java6中已经支持用Java开发WebService应用了,而且很方便。这样就大大减少了项目安装部署的代价,因为选择开源的框架依赖大量第三方包,程序的尺寸倍增。
下面是一个Java6开发Web Service的入门例子。
package org.agrimony.ws.server; import javax.jws.WebService; import javax.xml.ws.Endpoint; /** * Java6开发WebService入门 * * @author agrimony * */ @WebService public class Java6WebService { /** * Web服务中的业务方法 * * @return */ public String doSomething() { return "Hello Java6 WebService!"; }; /** * @param args */ public static void main(String[] args) { // 发布一个WebService Endpoint.publish("http://192.168.2.126:8080/WebService/Java6WebService", new Java6WebService()); } }
运行后,在浏览器中访问http://192.168.2.126:8080/WebService/Java6WebService?wsdl会得到如下结果:
This XML file does not appear to have any style information associated with it. The document tree is shown below. <!-- 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://server.ws.agrimony.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.agrimony.org/" name="Java6WebServiceService"> <types> <xsd:schema> <xsd:import namespace="http://server.ws.agrimony.org/" schemaLocation="http://192.168.2.126:8080/WebService/Java6WebService?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="Java6WebService"> <operation name="doSomething"> <input message="tns:doSomething"/> <output message="tns:doSomethingResponse"/> </operation> </portType> <binding name="Java6WebServicePortBinding" type="tns:Java6WebService"> <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="Java6WebServiceService"> <port name="Java6WebServicePort" binding="tns:Java6WebServicePortBinding"> <soap:address location="http://192.168.2.126:8080/WebService/Java6WebService"/> </port> </service> </definitions>