Java6开发WebService入门

来源:互联网 发布:企业级服务软件 编辑:程序博客网 时间:2024/05/17 06:30
Java6开发WebService入门
 
之前常常用CXF、Axis2、XFire等来开发结合Java语言来开发Web Service应用,这样的好处是用途广,灵活,另外一个重要原因是我们的生产环境是Java5。
但实际上Java6中已经支持用Java开发WebService应用了,而且很方便。这样就大大减少了项目安装部署的代价,因为选择开源的框架依赖大量第三方包,程序的尺寸倍增。
 
下面是一个Java6开发Web Service的入门例子。
 
package lavasoft;

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

/**
* Java6开发WebService入门
*
* @author leizhimin 2009-11-13 16:10:44
*/

@WebService
public class Java6WebService {
        /**
         * Web服务中的业务方法
         *
         * @return 一个字符串
         */

        public String doSomething() {
                return "Hello Java6 WebService!";
        }

        public staticvoid main(String[] args) {
                //发布一个WebService
                Endpoint.publish("http://192.168.14.117:8080/java6ws/lavasoft.Java6WebService", new Java6WebService());
        }
}
 
运行后,在浏览器中访问http://192.168.14.117:8080/java6ws/lavasoft.Java6WebService?wsdl会得到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.    
-->
<definitionsxmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://lavasoft/"
                         xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/"
                         targetNamespace="http://lavasoft/"name="Java6WebServiceService">
        <types>
                <xsd:schema>
                        <xsd:importnamespace="http://lavasoft/"
                                                schemaLocation="http://192.168.14.117:8080/java6ws/lavasoft.Java6WebService?xsd=1"/>
                </xsd:schema>
        </types>
        <messagename="doSomething">
                <partname="parameters"element="tns:doSomething"/>
        </message>
        <messagename="doSomethingResponse">
                <partname="parameters"element="tns:doSomethingResponse"/>
        </message>
        <portTypename="Java6WebService">
                <operationname="doSomething">
                        <inputmessage="tns:doSomething"/>
                        <outputmessage="tns:doSomethingResponse"/>
                </operation>
        </portType>
        <bindingname="Java6WebServicePortBinding"type="tns:Java6WebService">
                <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"style="document"/>
                <operationname="doSomething">
                        <soap:operation soapAction=""/>
                        <input>
                                <soap:bodyuse="literal"/>
                        </input>
                        <output>
                                <soap:bodyuse="literal"/>
                        </output>
                </operation>
        </binding>
        <servicename="Java6WebServiceService">
                <portname="Java6WebServicePort"binding="tns:Java6WebServicePortBinding">
                        <soap:addresslocation="http://192.168.14.117:8080/java6ws/lavasoft.Java6WebService"/>
                </port>
        </service>
</definitions>
 
抓一个图片如下:
 
可见,Java6开发WebService也是很方便的。

出处http://lavasoft.blog.51cto.com/62575/226565
原创粉丝点击