Java6開發WebService入門
以前經常用CXF、Axis二、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());
}
}
<?
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
>
抓一個圖片以下:
可見,Java6開發WebService也是很方便的。