使用JDK开发webService

一、服务端开发

  1、定义SEI(WebService Endpoint Interface)

package webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface DoSomeThing{
    
    @WebMethod
    public String say(String message);
}

  其中接口上需要使用@WebService注解,而方法上需要使用@WebMethod

  2、定义SEI的实现类

package webservice;

import javax.jws.WebService;

@WebService
public class DoSomeThingImp implements DoSomeThing {

    @Override
    public String sayHello(String message) {
        System.out.println("Hello " + message);
        return "Hello " + message;
    }
}

  3、发布WebService服务

package webservice;

import javax.xml.ws.Endpoint;

public class Service {
    public static void main(String[] args) {
        Endpoint.publish("http://192.168.0.102/my_webService/doSomething", new DoSomeThingImp());
        System.out.println("发布web service服务成功");
    }
}

其中publish方法中第一个参数为发布服务的地址,第二个参数为SEI的实现类对象。打开浏览器,输入http://192.168.0.102/my_webService/doSomething?wsdl,若能正常打开wsdl则发明发布成功。

二、客户端

1、查看wsdl文档,在浏览器地址栏输入发布时的地址再加上?wsdl即可,如http://192.168.0.102/my_webService/doSomething?wsdl 浏览器中显示:

<!--
 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://webservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice/" name="DoSomeThingImpService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice/" schemaLocation="http://192.168.0.102/my_webService/doSomething?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="DoSomeThingImp">
<operation name="sayHello">
<input wsam:Action="http://webservice/DoSomeThingImp/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://webservice/DoSomeThingImp/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="DoSomeThingImpPortBinding" type="tns:DoSomeThingImp">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="DoSomeThingImpService">
<port name="DoSomeThingImpPort" binding="tns:DoSomeThingImpPortBinding">
<soap:address location="http://192.168.0.102/my_webService/doSomething"/>
</port>
</service>
</definitions>

2、使用wsimport生成客户端代码。

  wsimport为JDK工具,配置好Java环境变量后,可在直接在命令窗口直接运行。wsimport命令常用参数说明

    -keep:是否生成java源文件

    -d:指定.class文件的输出目录

    -s:指定.java文件的输出目录

    -p:定义生成类的包名,不定义的话有默认包名

    -verbose:在控制台显示输出信息

    -b:指定jaxws/jaxb绑定文件或额外的schemas

    -extension:使用扩展来支持SOAP1.2

  例如需要在e盘webservice目录下生成客户端代码,代码所在的包路径为webservice.client,且需要生成java源文件的命令如下:

wsimport -d e:\\webservice -p webservice.client -keep http://192.168.0.102/my_webService/doSomething?wsdl

  也可以直接将浏览器中的wsdl保到本地,例如将上面的可以保存为myService.wsdl文件,直接用本地文件生成客户端代码,只需将上面命令中对应的网络地址地为本地文件地址即可。

3、编写测试代码

 

package webservice.client;

public class ClientTest {
    public static void main(String[] args) {
        DoSomeThingImpService factory = new DoSomeThingImpService();
        DoSomeThingImp service = factory.getDoSomeThingImpPort();
        String result = service.sayHello("Jack");
     System.out.println(result); } }

运行代码,输出结果为:Hello Jack

  上述代码中factory的类型根据wsdl文件中service元素的name属性进行确定,而service类型为文件中的portType元素中的name属性,对应的对象类型为binding元素的name属性

posted on 2018-05-12 23:05  强仔005  阅读(121)  评论(0)    收藏  举报