最近项目要使用WebService,正在学习中,做个记录。

WebService是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。 巴拉巴拉。。。。。。

开始编写接口

package team.soi.service;

public interface HelloService {

    /**
     * to do sth.
     * @param to
     * @return
     */
    Object toDoSth(String to);
}

编写接口的实现类

package team.soi.service.impl;

import team.soi.service.HelloService;

import javax.jws.WebService;

@WebService
public class HelloServiceImpl implements HelloService {

    public Object toDoSth(String to) {
        return "Hello," + to + "! Welcome to my webservice world!";
    }
}

发布WebService

package team.soi;

import team.soi.service.impl.HelloServiceImpl;

import javax.xml.ws.Endpoint;

public class App {
    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8899/ws/demo", new HelloServiceImpl());
    }
}

奏是介么仍性,在浏览器输入http://localhost:8899/ws/demo?wsdl 见证奇迹的时刻到了,Duang……如下:

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 (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy"xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://impl.service.soi.team/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.soi.team/"name="HelloServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://impl.service.soi.team/" schemaLocation="http://localhost:8899/ws/demo?xsd=1"/>
</xsd:schema>
</types>
<message name="toDoSth">
<part name="parameters" element="tns:toDoSth"/>
</message>
<message name="toDoSthResponse">
<part name="parameters" element="tns:toDoSthResponse"/>
</message>
<portType name="HelloServiceImpl">
<operation name="toDoSth">
<input wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthRequest" message="tns:toDoSth"/>
<output wsam:Action="http://impl.service.soi.team/HelloServiceImpl/toDoSthResponse" message="tns:toDoSthResponse"/>
</operation>
</portType>
<binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="toDoSth">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloServiceImplService">
<port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding">
<soap:address location="http://localhost:8899/ws/demo"/>
</port>
</service>
</definitions>

到这里,说明你的WebService服务端编写完成了,接下来,我们要怎么去调用呢? 说真的,在今天之前,我就知道WebService可以如上的写法,但是不会调用,感觉自己好Low,于是乎,在群里面一吼,很多热心的人回答了我的问题,得知了接下来该做的事情。

生成Jar

在你的终端里,输入命令,如下:

soi@soi:~/workspace/wsc$ wsimport -extension -keep -p team.soi.ws.client -s ./src -d ./bin http://localhost:8899/ws/demo?wsdl
正在解析 WSDL...

正在生成代码...

正在编译代码...

soi@soi:~/workspace/wsc$ cd bin
soi@soi:~/workspace/wsc/bin$ jar cvf hello-ws-demo.jar team
soi@soi:~/workspace/wsc/bin$ ls
hello-ws-demo.jar  team

此时,我们已经拿到了本WebService的客户端jar包,我们将客户端jar包加入到我们的工程,顺理成章,开始编写客户端代码:

package team.soi;

import junit.framework.TestCase;
import team.soi.ws.client.HelloServiceImpl;
import team.soi.ws.client.HelloServiceImplService;

public class AppTest
        extends TestCase {

    /**
     * test ws
     */
    public void testWs() {
        HelloServiceImpl service = new HelloServiceImplService().getHelloServiceImplPort();
        String s = (String) service.toDoSth("Soi");
        System.out.println(s);
    }
}

运行测试代码:

Hello,Soi! Welcome to my webservice world!

撸完收工…….