Java6中已经支持用Java开发WebService应用了,而且很方便。这样就大大减少了项目安装部署的代价,因为选择开源的框架依赖大量第三方包,程序的尺寸倍增。
下面是一个Java6开发Web Service的入门例子。 extends SpringBeanAutowiringSupport 的作用是为了可以用@AutoWired注入
package com.mediacross.adnetwork.base.webservice;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.mediacross.adnetwork.base.service.RoleService;
@WebService
public class MessageService extends SpringBeanAutowiringSupport {
@Autowired
private RoleService roleService;
public String getReportData(String data) {
String returnData = "";
if(roleService==null){
return "aasdf";
}
try {
String[] dataStrs = data.split("&");
for (int i = 0; i < dataStrs.length; i++) {
returnData = returnData + "," + dataStrs[i].split("#")[0];
}
returnData = returnData.substring(1);
} catch (Exception e) {
returnData = "";
}
return returnData;
}
public String getMoData(String info) {
String returnData = "";
try {
String[] infoStrs = info.split("&");
for (int i = 0; i < infoStrs.length; i++) {
returnData = returnData + "," + infoStrs[i].split("#")[0];
}
returnData = returnData.substring(1);
} catch (Exception e) {
returnData = "";
}
return returnData;
}
public static void main(String[] args) {
//发布一个WebService
Endpoint.publish("http://192.168.1.8:8080/adnetwork/com.mediacross.adnetwork.base.webservice.MessageService", new MessageService());
}
}
运行后,在浏览器中访问 http://192.168.1.8:8080/adnetwork/com.mediacross.adnetwork.base.webservice.MessageService?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://webservice.base.adnetwork.mediacross.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.base.adnetwork.mediacross.com/" name="MessageServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.base.adnetwork.mediacross.com/" schemaLocation="http://192.168.1.8:8080/adnetwork/com.mediacross.adnetwork.base.webservice.MessageService?xsd=1"/>
</xsd:schema>
</types>
<message name="getReportData">
<part name="parameters" element="tns:getReportData"/>
</message>
<message name="getReportDataResponse">
<part name="parameters" element="tns:getReportDataResponse"/>
</message>
<message name="getMoData">
<part name="parameters" element="tns:getMoData"/>
</message>
<message name="getMoDataResponse">
<part name="parameters" element="tns:getMoDataResponse"/>
</message>
<portType name="MessageService">
<operation name="getReportData">
<input message="tns:getReportData"/>
<output message="tns:getReportDataResponse"/>
</operation>
<operation name="getMoData">
<input message="tns:getMoData"/>
<output message="tns:getMoDataResponse"/>
</operation>
</portType>
<binding name="MessageServicePortBinding" type="tns:MessageService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getReportData">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getMoData">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MessageServiceService">
<port name="MessageServicePort" binding="tns:MessageServicePortBinding">
<soap:address location="http://192.168.1.8:8080/adnetwork/com.mediacross.adnetwork.base.webservice.MessageService"/>
</port>
</service>
</definitions>