最近一段时间开始重新学习Web的知识,去年好长一段时间没做这个了。最近有个项目要调用WebService接口,所以就花时间学习了一下Soap接口。开始时候只是调用接口所以就使用Axis生成本地可调用的文件,这样调用接口是没有问题,现在顺便学一下服务端的写法,就做了一个Demo试了一下,用的是MyEclipse7.0和Tomcat6.0,一切顺利。
1 新建一个WebService Project,项目名称TestJaxWs;
2 新建一个Java文件TestWebService.java,如下:
package com.zhou.testWebService;
import java.util.ArrayList;import java.util.List;
public class TestWebService {
public String SayHello(String name){ return "hello "+name; } public int add(int a,int b){ return a+b; } public List<String> GetSomeThing(){ List<String> list = new ArrayList<String>(); for(int i=0;i<10;i++){ list.add("test"+i); } return list; }}
3 在项目上点右键New一个WebService,单击下一步,选择Create WebService from Java class,单击下一步选择Java Class,点击Finish完成,MyEclipse会在和你刚才缩写的类同一目录下新建一个TestWebServiceDelegate.java文件,同事会在WebRoot下生成一些文件和配置。
4 把此项目部属到Tomcat中,启动服务。
5 在地址栏中输入地址http://localhost:8080/TestJaxWs/TestWebServicePort?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.3-hudson-390-. --> - < definitions xmlns =" http://schemas.xmlsoap.org/wsdl/ " xmlns:soap =" http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:tns =" http://testWebService.zhou.com/ " xmlns:wsu =" http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " name =" TestWebServiceService " targetNamespace =" http://testWebService.zhou.com/ "> - < types > - < xsd:schema > < xsd:import namespace =" http://testWebService.zhou.com/ " schemaLocation =" http://localhost:8080/TestJaxWs/TestWebServicePort?xsd=1 " /> </ xsd:schema > </ types > - < message name =" add "> < part element =" tns:add " name =" parameters " /> </ message > - < message name =" addResponse "> < part element =" tns:addResponse " name =" parameters " /> </ message > - < message name =" SayHello "> < part element =" tns:SayHello " name =" parameters " /> </ message > - < message name =" SayHelloResponse "> < part element =" tns:SayHelloResponse " name =" parameters " /> </ message > - < message name =" GetSomeThing "> < part element =" tns:GetSomeThing " name =" parameters " /> </ message > - < message name =" GetSomeThingResponse "> < part element =" tns:GetSomeThingResponse " name =" parameters " /> </ message > - < portType name =" TestWebServiceDelegate "> - < operation name =" add "> < input message =" tns:add " /> < output message =" tns:addResponse " /> </ operation > - < operation name =" SayHello "> < input message =" tns:SayHello " /> < output message =" tns:SayHelloResponse " /> </ operation > - < operation name =" GetSomeThing "> < input message =" tns:GetSomeThing " /> < output message =" tns:GetSomeThingResponse " /> </ operation > </ portType > - < binding name =" TestWebServicePortBinding " type =" tns:TestWebServiceDelegate "> < soap:binding style =" document " transport =" http://schemas.xmlsoap.org/soap/http " /> - < operation name =" add "> < soap:operation soapAction ="" /> - < input > < soap:body use =" literal " /> </ input > - < output > < soap:body use =" literal " /> </ output > </ operation > - < operation name =" SayHello "> < soap:operation soapAction ="" /> - < input > < soap:body use =" literal " /> </ input > - < output > < soap:body use =" literal " /> </ output > </ operation > - < operation name =" GetSomeThing "> < soap:operation soapAction ="" /> - < input > < soap:body use =" literal " /> </ input > - < output > < soap:body use =" literal " /> </ output > </ operation > </ binding > - < service name =" TestWebServiceService "> - < port binding =" tns:TestWebServicePortBinding " name =" TestWebServicePort "> < soap:address location =" http://localhost:8080/TestJaxWs/TestWebServicePort " /> </ port > </ service > </ definitions >至此,一个简单的WebService编写并发布完成,只要通过你发布的WebService的地址,就可访问次服务。