之前常常用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());
}
}
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 >
<!-- 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也是很方便的。
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/226565