JAX-WS:wsgen工具示例

wsgen工具用于解析现有的Web服务实现类,并生成用于Web服务部署的必需文件(JAX-WS可移植工件)。 该wsgen工具位于$JDK/bin文件夹中。

用例

wsgen工具的2个常见用例:

  1. 生成用于Web服务部署的JAX-WS可移植工件(Java文件)。
  2. 生成WSDL和xsd文件,以进行测试或Web服务客户端开发。

让我们看一个简单的Web服务实现类,只是一个返回字符串的方法。

文件:ServerInfo.java

package com.mkyong.ws;

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

@WebService
public class ServerInfo{

	@WebMethod
	public String getIpAddress() {
		
		return "10.10.10.10";
		
	}

}

1.生成JAX-WS可移植工件(Java文件)

要为上述Web服务实现类( ServerInfo.java )生成所有JAX-WS可移植工件,请使用以下命令:

命令:wsgen用法

D:\>wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo

Note:   ap round: 1
[ProcessedMethods Class: com.mkyong.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): com.mkyong.ws.ServerInfo]
[requestWrapper: com.mkyong.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
com\mkyong\ws\jaxws\GetIpAddress.java
com\mkyong\ws\jaxws\GetIpAddressResponse.java
Note:   ap round: 2

在这种情况下,它生成了四个文件:

  1. com \ mkyong \ ws \ jaxws \ GetIpAddress.java
  2. com \ mkyong \ ws \ jaxws \ GetIpAddress.class
  3. com \ mkyong \ ws \ jaxws \ GetIpAddressResponse.java
  4. com \ mkyong \ ws \ jaxws \ GetIpAddressResponse.class

文件:GetIpAddress.java

package com.mkyong.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.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getIpAddress", namespace = "http://ws.mkyong.com/")
public class GetIpAddress {

}

文件:GetIpAddressResponse.java

package com.mkyong.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.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getIpAddressResponse", namespace = "http://ws.mkyong.com/")
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.生成WSDL和xsd

要为上述Web服务实现类( ServerInfo.java )生成WSDL和xsd文件, wsgen命令中添加一个额外的wsgen

命令:wsgen用法

D:\>wsgen -verbose -keep -cp . com.mkyong.ws.ServerInfo -wsdl

Note:   ap round: 1
[ProcessedMethods Class: com.mkyong.ws.ServerInfo]
[should process method: getIpAddress hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: getIpAddress()]
[method.getDeclaringType(): com.mkyong.ws.ServerInfo]
[requestWrapper: com.mkyong.ws.jaxws.GetIpAddress]
[ProcessedMethods Class: java.lang.Object]
com\mkyong\ws\jaxws\GetIpAddress.java
com\mkyong\ws\jaxws\GetIpAddressResponse.java
Note:   ap round: 2

在这种情况下,它生成了六个文件:

  1. com \ mkyong \ ws \ jaxws \ GetIpAddress.java
  2. com \ mkyong \ ws \ jaxws \ GetIpAddress.class
  3. com \ mkyong \ ws \ jaxws \ GetIpAddressResponse.java
  4. com \ mkyong \ ws \ jaxws \ GetIpAddressResponse.class
  5. ServerInfoService_schema1.xsd
  6. ServerInfoService.wsdl

文件:ServerInfoService_schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" 
targetNamespace="http://ws.mkyong.com/" 
xmlns:tns="http://ws.mkyong.com/" 
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>

文件:ServerInfoService.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions targetNamespace="http://ws.mkyong.com/" 
name="ServerInfoService" xmlns="http://schemas.xmlsoap.org/wsdl/" 
xmlns:tns="http://ws.mkyong.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://ws.mkyong.com/" 
	         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 message="tns:getIpAddress"/>
      <output 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>

发表了!

所有文件都准备好了,可以通过终结点发布者进行发布。

package com.mkyong.endpoint;
 
import javax.xml.ws.Endpoint;
import com.mkyong.ws.ServerInfo;

//Endpoint publisher
public class WsPublisher{
 
	public static void main(String[] args) {
	   Endpoint.publish("http://localhost:8888/ws/server", new ServerInfo());
	   
	   System.out.println("Service is published!");
    }
 
}

下载源代码

下载它– JAX-WS-wsgen-command-example.zip (5KB)

参考

  1. 官方网站:wsgen JavaDoc

翻译自: https://mkyong.com/webservices/jax-ws/jax-ws-wsgen-tool-example/

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值