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 the wsgen
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
<?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>
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!");
}
}
I had modify the generated xsd file said minOccurs change to 1. But how can I publish it on
http://127.0.0.1:9999/ws/hello?wsdl
It always publish the default wsdl and xsd back to me. the one I change never display. Where should I put the wsdl and xsd files under by file system?
the element and complextype tags are separately created in Schema file, how can they be created one inside the other?
Thank you.
Thanks for your valuable posting.I have collect more than information from your website. It is really wonderful blog. please added more than tips. i’m working in Erp in india.Here providing very low price and Quality ERP,Cloud ERP, CMS , responsive webdesign and ERP. you have any more than information kindly make me call this number 044-42127512 or send your mail [email protected].
How can we have the schema included within the WSDL. I dnt wish to have a seperate schema file.Is there a way to specify that.
change the SoapBinding style to RPC then all the types will be included in WSDL itself.
What If I have My Own JaxB Classes,How to create the XSD from those JAXB Classes,using WSGEN,
I know there are couple of tools to generate XSD file from JAXB classes,But I want to create those XSD file from my own already created JaxB classes,during the generation of WSDL using wsgen.
Thanks in Advance
hi, I have a problem when i execute the command wsgen
The path of my java project is “E:\java\Server\src\com\mkyong\ws\ServerInfo.java”
I use the wsgen commands as:
E:\>wsgen -verbose -keep -cp E:\java\Server\src\com.mkyong.ws.ServerInfo
error:
Missing SEI
Could you help me resolve?Thanks
Follow the tutorial here: http://www.youtube.com/watch?v=od6fNiegu-Q
it will guide you through the eclipse in how to use the wsgen comment to generate the WSDL…
A very good example. I succeded to create and run my first webservice after following this simple and straight forward example(I was struggling to create my helloworld type simple webservice). Thanks mr. MKYong.
Hi,
i am using the wsgen command and i am getting the following output on command promp but no source files or classes are getting generated and pointers on this…
C:\Program Files\Java\jdk1.6.0_31\bin>wsgen -verbose -keep -cp . Service.AddSer
viceImpl -d src
Note: ap round: 1
[ProcessedMethods Interface: Service.AddService]
[should process method: addNumber hasWebMethods: false ]
[endpointReferencesInterface: true]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen – method: addNumber(int,int)]
[method.getDeclaringType(): Service.AddService]
remove the “parameterStyle = ParameterStyle.BARE”
Hi MKYong,
I’m encountering a wsgen compilation failure for Hibernate Annotations.
Scenario – Create JAX-WS webservice for validation of a user.
Issue Faced – wsgen command fails with below error
error: Could not create declaration for annotation type com.
error: Could not create declaration for annotation type com.
2 errors error: compilation failed, errors should have been reported
Command-
wsgen -s src/main/java -d target/Struts2Example/WEB-INF/classes -cp target/Struts2Example/WEB-INF/classes com.hcl.customer.dao.impl.WSCustomerDAOImpl
Code –
@WebService(endpointInterface=”com.hcl.customer.dao.WSCustomerDAO”)
public class WSCustomerDAOImpl implements WSCustomerDAO{
Logger logger = Logger.getLogger(WSCustomerDAOImpl.class);
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
// return customer model if the username & password exists
public Customer validateUser(String username, String password)
throws Exception {
Criteria criteria = session
.createCriteria(Customer.class)
.add(Restrictions
.conjunction()
.add(Restrictions.eq(“username”, username))
.add(Restrictions.eq(“password”,
EncryptionDecryptionUtility.encrypt(password))));
Customer customer = (Customer) criteria.uniqueResult();
if(customer != null)
logger.info(“In validateUser()” + customer.toString());
return customer;
}
}
StackTrace-
error: Could not create declaration for annotation type com.
error: Could not create declaration for annotation type com.
2 errors
error: compilation failed, errors should have been reported
Regards,
PD
I am getting the below exception while running your file. may I know where is the issue?
–
–
–
0
–
java.lang.ArrayIndexOutOfBoundsException: 0 at org.apache.axis2.description.AxisService.getWSDL(AxisService.java:1643) at org.apache.axis2.description.AxisService.printWSDL(AxisService.java:1413) at org.apache.axis2.transport.http.HTTPWorker.service(HTTPWorker.java:154) at org.apache.axis2.transport.http.server.AxisHttpService.doService(AxisHttpService.java:281) at org.apache.axis2.transport.http.server.AxisHttpService.handleRequest(AxisHttpService.java:187) at org.apache.axis2.transport.http.server.HttpServiceProcessor.run(HttpServiceProcessor.java:82) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
Its in the browser, after publishing the WS.
hi, i am having a problem when i execute the command wsgen
C:\Users\ominatti\Documents\JAVA\JWS\EXAMPLE WSGEN\WebServices>wsgen -verbose -k
eep -cp . com.mkyong.ws.ServerInfo
error
Class not found: “com.mkyong.ws.ServerInfo”
Could you help me please ¿?
Thanks
You must have com.mkyong.ws.ServerInfo class file after compilation(javac)
Please Anyone Advice me ..
How to annotate this claas to create web service.
As I found null value in list type field from web service.
@WebMethod(operationName = “getTickets”)
public List getTickets(long ticketId) throws TicketingException {
try {
return new TicketBO().getTickets(ticketId);
} catch (TicketingException e) {
logger.error(e);
throw e;
}
}
public class Ticket{
private Long ticketId;
private String callerNumber;
private List dynaFieldValues;
}
public class DynaFieldValue implements Serializable,Comparable {
private DynaFieldValueId id;
private String value;
}
Hi
Thanks for nice tutorial, Is it possible to generate wsdl with inline schema,
is there way we can automate generation of wsdl with inline (instead of having separate schema file)
Many Thanks in advance 🙂
Just a little side-note: before using wsgen tool, web service implementation class must be compiled (using javac).
🙂