Инструмент
wsgen
используется для анализа существующего класса реализации веб-службы и создает необходимые файлы (переносимые артефакты JAX-WS) для развертывания веб-службы. Этот инструмент
wsgen
доступен в папке` $ JDK/bin`.
Варианты использования
2 распространенных варианта использования инструмента wsgen:
, Генерирует переносимые артефакты JAX-WS (файлы Java) для веб-службы.
развертывание.
, Создает файлы WSDL и xsd для тестирования или клиента веб-службы.
развитие.
Давайте посмотрим класс реализации веб-сервиса, довольно простой, просто метод для возврата строки.
Файл: Сервер In.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)
Чтобы сгенерировать все переносимые артефакты JAX-WS для указанного выше класса реализации веб-службы (
ServerInfo.java
), используйте следующую команду:
Command: использование 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
В этом случае было сгенерировано четыре файла:
, ком \ mkyong \ WS \ JAXWS \ GetIpAddress.java
, ком \ mkyong \ WS \ JAXWS \ GetIpAddress.class
, ком \ mkyong \ WS \ JAXWS \ GetIpAddressResponse.java
, ком \ mkyong \ WS \ JAXWS \ GetIpAddressResponse.class
File: 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 { }
File: 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. Genarates WSDL и xsd
Чтобы сгенерировать файлы WSDL и xsd для указанного выше класса реализации веб-службы (
ServerInfo.java
), добавьте дополнительный
-wsdl
в команду` wsgen`
Command: использование 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
В этом случае было сгенерировано шесть файлов:
, ком \ mkyong \ WS \ JAXWS \ GetIpAddress.java
, ком \ mkyong \ WS \ JAXWS \ GetIpAddress.class
, ком \ mkyong \ WS \ JAXWS \ GetIpAddressResponse.java
, ком \ mkyong \ WS \ JAXWS \ GetIpAddressResponse.class
, ServerInfoService__schema1.xsd
, ServerInfoService.wsdl
File: 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>
File: 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!"); } }
Скачать исходный код
Загрузить - ссылка://wp-content/uploads/2010/12/JAX-WS-wsgen-command-example.zip[JAX-WS-wsgen-command-example.zip](5 КБ)
Ссылка
, http://jax-ws.java.net/nonav/2.1.2/docs/wsgen.html [Official site:
wsgen JavaDoc]
ссылка://тег/jax-ws/[jax-ws]ссылка://тег/мыло/[soap]ссылка://тег/веб-сервисы/[веб-сервисы]ссылка://тег/wsgen/[wsgen]