Java6開發WebService入門

來源:互聯網
上載者:User
 之前常常用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 static void 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.    
-->
<definitions xmlns: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:import namespace="http://lavasoft/"
                                                schemaLocation="http://192.168.14.117:8080/java6ws/lavasoft.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.14.117:8080/java6ws/lavasoft.Java6WebService"/>
                </port>
        </service>
</definitions> 抓一個圖片如下:{
window.open("http://blog.51cto.com/viewpic.php?refimg=" + this.src)
}' src="http://img1.51cto.com/attachment/200911/200911131258100600343.png" border="0" alt="" width="650"> 可見,Java6開發WebService也是很方便的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.