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》