web 项目生成webservice 服务端 和 客户端

The wsgen tool is used to parse an existing web service implementation class and generates required files (JAX-WS portable artifacts) for web service deployment. This wsgen tool is available in $JDK/bin folder.

Use cases

2 common use cases for wsgen tool :

Generates JAX-WS portable artifacts (Java files) for web service deployment.
Generates WSDL and xsd files, for testing or web service client development.

Let’s see a web service implementation class, quite simple, just a method to return a string.

File : ServerInfo.java
(在N:\workshop\webservices>这个目录创建文件package: net.cublog.ws
N:\workshop\webservices\net\cublog\ws\ServerInfo.java)


package net.cublog.ws;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

 

@WebService

public class ServerInfo{

 

    @WebMethod

    public String getIpAddress() {

 

        return "10.10.10.10";

 

    }

 

}

1. Generates JAX-WS portable artifacts (Java files)

To generate all the JAX-WS portable artifacts for above web service implementation class (ServerInfo.java), use following command :

Command : wsgen usage
N:\workshop\webservices>cd N:\workshop\webservices\net\cublog\ws


N:\workshop\webservices\net\cublog\ws>javac ServerInfo.java


N:\workshop\webservices\net\cublog\ws>cd ..


N:\workshop\webservices\net\cublog>cd ..


N:\workshop\webservices\net>cd ..


N:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo


warning: The apt tool and its associated API are planned to be
removed in the next major JDK release. These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model. Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note: ap round: 1
[ProcessedMethods Class: net.cublog.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): net.cublog.ws.ServerInfo]
[requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
net\cublog\ws\jaxws\GetIpAddress.java
net\cublog\ws\jaxws\GetIpAddressResponse.java
Note: ap round: 2


N:\workshop\webservices>
In this case, it generated four files :
N:\workshop\webservices\net\cublog\ws\jaxws
GetIpAddress.java
GetIpAddress.class
GetIpAddressResponse.java
GetIpAddressResponse.class

File : GetIpAddress.java
package net.cublog.ws.jaxws;



import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;



@XmlRootElement(name = "getIpAddress", namespace = "http://ws.cublog.net/")

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "getIpAddress", namespace = "http://ws.cublog.net/")

public class GetIpAddress {





}

File:GetIpAddressResponse.java
package net.cublog.ws.jaxws;



import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlType;



@XmlRootElement(name = "getIpAddressResponse", namespace = "http://ws.cublog.net/")

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "getIpAddressResponse", namespace = "http://ws.cublog.net/")

public class GetIpAddressResponse {



    @XmlElement(name = "return", namespace = "")

    private String _return;



    /**

     * 

     * @return

     * returns String

     */

    public String getReturn() {

        return this._return;

    }



    /**

     * 

     * @param _return

     * the value for the _return property

     */

    public void setReturn(String _return) {

        this._return = _return;

    }



}



2. Genarates WSDL and xsd

To generate WSDL and xsd files for above web service implementation class (ServerInfo.java), add an extra -wsdl in the wsgen command :

Command : wsgen usage
N:\workshop\webservices>wsgen -verbose -keep -cp . net.cublog.ws.ServerInfo -wsdl


warning: The apt tool and its associated API are planned to be
removed in the next major JDK release. These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model. Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note: ap round: 1
[ProcessedMethods Class: net.cublog.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): net.cublog.ws.ServerInfo]
[requestWrapper: net.cublog.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
net\cublog\ws\jaxws\GetIpAddress.java
net\cublog\ws\jaxws\GetIpAddressResponse.java
Note: ap round: 2


N:\workshop\webservices>
In this case, it generated six files :
N:\workshop\webservices\net\cublog\ws\jaxws
GetIpAddress.java
GetIpAddress.class
GetIpAddressResponse.java
GetIpAddressResponse.class
N:\workshop\webservices\ServerInfoService_schema1.xsd
N:\workshop\webservices\ServerInfoService.wsdl

File : ServerInfoService.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. -->
<definitions targetNamespace="http://ws.cublog.net/" name="ServerInfoService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:tns="http://ws.cublog.net/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://ws.cublog.net/" schemaLocation="ServerInfoService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="getIpAddress">
    <part name="parameters" element="tns:getIpAddress"/>
  </message>
  <message name="getIpAddressResponse">
    <part name="parameters" element="tns:getIpAddressResponse"/>
  </message>
  <portType name="ServerInfo">
    <operation name="getIpAddress">
      <input wsam:Action="http://ws.cublog.net/ServerInfo/getIpAddressRequest" message="tns:getIpAddress"/>
      <output wsam:Action="http://ws.cublog.net/ServerInfo/getIpAddressResponse" message="tns:getIpAddressResponse"/>
    </operation>
  </portType>
  <binding name="ServerInfoPortBinding" type="tns:ServerInfo">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="getIpAddress">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="ServerInfoService">
    <port name="ServerInfoPort" binding="tns:ServerInfoPortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>

File:ServerInfoService_schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" targetNamespace="http://ws.cublog.net/" xmlns:tns="http://ws.cublog.net/" xmlns:xs="http://www.w3.org/2001/XMLSchema">



  <xs:element name="getIpAddress" type="tns:getIpAddress"/>



  <xs:element name="getIpAddressResponse" type="tns:getIpAddressResponse"/>



  <xs:complexType name="getIpAddress">

    <xs:sequence/>

  </xs:complexType>



  <xs:complexType name="getIpAddressResponse">

    <xs:sequence>

      <xs:element name="return" type="xs:string" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:schema>

Published It!


接下来是myeclipse操作步骤:

 new web service  --------

web 项目生成webservice 服务端 和 客户端_第1张图片

 

web 项目生成webservice 服务端 和 客户端_第2张图片

 

 

web 项目生成webservice 服务端 和 客户端_第3张图片

 

 

然后编辑    这个文件,写代码

你可能感兴趣的:(web 项目生成webservice 服务端 和 客户端)