Java学习笔记——Java6开发WebService进阶

在上文中,使用Java6做了一个最简单的WebService服务的实现,并通过Java6API发布了该服务。
在本文中,将看到如何使用Java6来做一个WebService服务,并如何使用Java6提供的开发工具来生成客户端代码,并调用服务。
 
一、服务端代码
package org.agrimony.ws.server;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/**
 * Java6开发WebService入门
 * 
 * @author agrimony
 * 
 */
@WebService
public class Java6WS {
    /**
     * Web服务中的业务方法
     * 
     * @return
     */
    public String doSomething(String userName) {
        return userName+" is doing something!";
    };

    /**
     * @param args
     */
    public static void main(String[] args) {
        // 发布一个WebService
        Endpoint.publish("http://192.168.2.126:8080/WebService/Java6WS",
                new Java6WS());

    }

}

在浏览器中访问WSDL:http://192.168.2.126:8080/WebService/Java6WS?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://server.ws.agrimony.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.ws.agrimony.org/" name="Java6WSService">
<types>
<xsd:schema>
<xsd:import namespace="http://server.ws.agrimony.org/" schemaLocation="http://192.168.2.126:8080/WebService/Java6WS?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="Java6WS">
<operation name="doSomething">
<input message="tns:doSomething"/>
<output message="tns:doSomethingResponse"/>
</operation>
</portType>
<binding name="Java6WSPortBinding" type="tns:Java6WS">
<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="Java6WSService">
<port name="Java6WSPort" binding="tns:Java6WSPortBinding">
<soap:address location="http://192.168.2.126:8080/WebService/Java6WS"/>
</port>
</service>
</definitions>
二、生成客户端代码
 
Java6提供生成WebService客户端代码工具,注意,在生成前需要先启动服务端,用法如下图:
命令:D:\Program Files\Eclipse_JEE\workspace\Java6WS\src>wsimport  -p  org.agrimony.ws.client  -keep  http://192.168.2.126:8080/WebService/Java6WS?wsdl

期中"-p" 后边接要生成的客户端代码的命令空间,wsimport用法格式:wsimport [options] <WSDL_URI>

生成后的项目目录如下:

三、写客户端的测试类

package org.agrimony.ws.test;

import org.agrimony.ws.client.Java6WS;
import org.agrimony.ws.client.Java6WSService;

public class TestClient {

    /**
     * 测试Java6 WS生成的客户端代码
     * 
     * @param args
     */
    public static void main(String[] args) {
        // 创建一个客户端服务对象
        Java6WS java6ws = new Java6WSService().getJava6WSPort();
        // 调用服务方法,并得到方法返回值
        String returnContent = java6ws.doSomething("旺财");
        // 打印服务的返回值
        System.out.println(returnContent);
    }

}

注意,上面导入的类全是

org.agrimony.ws.client

包下面的。

调用输出结果:

旺财 is doing something!

 

参考原文地址:http://lavasoft.blog.51cto.com/62575/226581

 
posted @ 2013-08-12 15:57  Agrimony  阅读(421)  评论(0)    收藏  举报