JAX-WS : Wsgen Tool Example
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
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. 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
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
In this case, it generated four files :
- com\mkyong\ws\jaxws\GetIpAddress.java
- com\mkyong\ws\jaxws\GetIpAddress.class
- com\mkyong\ws\jaxws\GetIpAddressResponse.java
- com\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 and xsd
To generate WSDL and xsd files for above web service implementation class (
ServerInfo.java
), add an extra -wsdl in thewsgen
command :
Command : wsgen usage
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
In this case, it generated six files :
- com\mkyong\ws\jaxws\GetIpAddress.java
- com\mkyong\ws\jaxws\GetIpAddress.class
- com\mkyong\ws\jaxws\GetIpAddressResponse.java
- com\mkyong\ws\jaxws\GetIpAddressResponse.class
- ServerInfoService_schema1.xsd
- ServerInfoService.wsdl
File : ServerInfoService_schema1.xsd
version="1.0" encoding="UTF-8" standalone="yes"?>
version="1.0"
targetNamespace="http://ws.mkyong.com/"
xmlns:tns="http://ws.mkyong.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
name="getIpAddress" type="tns:getIpAddress"/>
name="getIpAddressResponse" type="tns:getIpAddressResponse"/>
name="getIpAddress">
/>
File : ServerInfoService.wsdl
version="1.0" encoding="UTF-8" standalone="yes"?>
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/">
namespace="http://ws.mkyong.com/"
schemaLocation="ServerInfoService_schema1.xsd"/>
Published It!
All files are ready, publish it via endpoint publisher.
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!"); } }
Download Source Code
Download It – JAX-WS-wsgen-command-example.zip (5KB)
No comments:
Post a Comment