【WebService学习】-----使用wsgen工具构建JAX-WS的WebService

本文档介绍了如何利用JDK自带的wsgen工具创建JAX-WS WebService。通过注解定义服务实现类,wsgen会生成所需的JAX-WS便携式文件、WSDL和XSD文件。详细步骤包括编写服务实现类,使用wsgen命令生成文件,以及发布服务。参考资源包含JDK官方文档和相关书籍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 简介

JDK中自带的工具wsgen为构建JAX-WS的WebService提供了便利,我们需要使用JAX-WS的注解编写一个服务实现类,wsgen工具将会解析服务实现类并且生成构建服务的必要文件。wsgen工具目录$jdk/bin在文件下面
wegen工具可以生成文件如下:
1.为构建WebService服务生成JAX-WS便携式文件。
2.生成WSDL和XSD文件,用于测试和web服务客户端开发。

2. 介绍

1.编写一个简单的实现类AccountService

package com.corp.test;

import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class AccountService {
  
  @WebMethod
  public String getAccount() {
    return "hello";
  }
}

2.在项目的AccountService.java所在的目录下,使用使用命令javac AccountService.java编译生成AccountService.class文件,然后回到项目的java目录下面。使用命令wsgen -verbose -keep -cp . com.corp.test.AccountService,为AccountService实现服务类生成发布JAX-WS服务需要的类,如下:

D:\newWorkspace\WebServiceTest\src\main\java>cd com/corp/test
D:\newWorkspace\WebServiceTest\src\main\java\com\corp\test>javac AccountService.java
D:\newWorkspace\WebServiceTest\src\main\java\com\corp\test>
D:\newWorkspace\WebServiceTest\src\main\java>wsgen -verbose -keep -cp  .  com.corp.test.AccountService
com\corp\test\jaxws\GetAccount.java
com\corp\test\jaxws\GetAccountResponse.java

生成文件的如下:
GetAccount.class
GetAccount.java
GetAccountResponse.class
GetAccountResponse.java

———GetAccount.java

package com.corp.test.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 = "getAccount", namespace = "http://test.corp.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getAccount", namespace = "http://test.corp.com/")
public class GetAccount {

}

———GetAccountResponse.java

package com.corp.test.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 = "getAccountResponse", namespace = "http://test.corp.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getAccountResponse", namespace = "http://test.corp.com/")
public class GetAccountResponse {

    @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;
    }

}

3.同样可以使用命令wsgen -verbose -keep -cp . com.corp.test.AccountService -wsdl来生成WSDL文件和XSD文件。

D:\newWorkspace\WebServiceTest\src\main\java>wsgen -verbose -keep -cp  .  com.corp.test.AccountService -wsdl
com\corp\test\jaxws\GetAccount.java
com\corp\test\jaxws\GetAccountResponse.java

生成文件的如下:
AccountServiceService.wsdl
AccountServiceService_schema1.xsd

———AccountServiceService.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- 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 targetNamespace="http://test.corp.com/" name="AccountServiceService" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="http://test.corp.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://test.corp.com/" schemaLocation="AccountServiceService_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="getAccount">
    <part name="parameters" element="tns:getAccount"/>
  </message>
  <message name="getAccountResponse">
    <part name="parameters" element="tns:getAccountResponse"/>
  </message>
  <portType name="AccountService">
    <operation name="getAccount">
      <input wsam:Action="http://test.corp.com/AccountService/getAccountRequest" message="tns:getAccount"/>
      <output wsam:Action="http://test.corp.com/AccountService/getAccountResponse" message="tns:getAccountResponse"/>
    </operation>
  </portType>
  <binding name="AccountServicePortBinding" type="tns:AccountService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="getAccount">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="AccountServiceService">
    <port name="AccountServicePort" binding="tns:AccountServicePortBinding">
      <soap:address location="REPLACE_WITH_ACTUAL_URL"/>
    </port>
  </service>
</definitions>

———AccountServiceService_schema1.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://test.corp.com/" xmlns:tns="http://test.corp.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

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

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

  <xs:complexType name="getAccount">
    <xs:sequence/>
  </xs:complexType>

  <xs:complexType name="getAccountResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

4.发布服务。

public class AccountServicePublisher {
  public static void main(String[] args) {
    Endpoint.publish("http://localhost:8887/AccountService", new AccountService());
    System.out.println("Service is published");
  }
}
3. 参考

1.JDK官方文档
2.《Spring IN ACTION》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值