In this session, we will learn, how to write the java web services with help of wsgen utility in jdk1.6+ and JAX-WS.
What we need before start the code, I'm using as mention below and already installed in my system:
1. JDK1.6 or +
2. Eclipse
3. Windows OS
4. Basic knowledge of java
Let's start:
Verify the JDK installation
Open the command prompt and verify the java installationType the
C:\>javac -version
javac 1.7.0_25
C:\>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)
Now java installation got verified, as installed JDK1.7.0_25
Verify the Eclipse installation
Go the the installation directory of eclipse and click on the eclipse.exee.g C:\eclipse\eclipse-java-mars-1-win32\eclipse\eclipse.exe
Eclipse installation got verified.
Open the Eclipse
Create the new java project
File
New
Java Project
Type the name of project:
JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
Click on the Next,
Change the bin to class as mention bellow image.
Click Finish.
Project structure look like
\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
- src
- JRE System Library[JavaSE-1.7]
Now, new java project creation done in Eclipse
Write the Service Class, AToZExamplesService.java
Right click on the src under project name \JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach.
New
Class
Type the name of package: com.atozexamples
Type the name of Class: AToZExamplesServices
Click the Finish
package com.atozexamples;
public class AToZExamplesService
{
public
AToZExamplesService() {
// TODO Auto-generated
constructor stub
}
}
Annotate service endpoint class with JAX-WS annotations
AToZExamplesService.java
Must have to follow certain rules at the time of creating the service endpoint class
- The service endpoint class should public and should not Final and Abstract
- The service endpoint class must have a default public constructor
- The service endpoint class must not define the finalize method
package com.atozexamples;
import javax.jws.WebMethod;
import
javax.jws.WebService;
@WebService(name = "AToZExamplesService", targetNamespace = "http://atozexample.com/AToZExamplesService")
public class AToZExamplesService
{
public
AToZExamplesService() {
// TODO Auto-generated
constructor stub
}
@WebMethod
public String
printGreeting() {
return "Hello from
AToZExamples.com!!";
}
}
Service end point class got annotated and added new method printGreeting()
Generate JAXWS artifacts using wsgen tool
Endpoint service class is ready. Now you have to generate all of the portable artifacts for a JAX-WS web service. JDK1.6 have wsgen tool to generate all this stuff.
Go to the command prompt and fire wsgen command
C:\>wsgen
Missing SEI
Usage: WSGEN [options] <SEI>
where [options] include:
-classpath <path> specify where to find input class files
-cp <path> same as -classpath <path>
-d <directory> specify where to place generated output files
-extension allow vendor extensions - functionality not specified
by the specification. Use of extensions may
result in applications that are not portable or
may not interoperate with other implementations
-help display help
-keep keep generated files
-r <directory> resource destination directory, specify where to
place resouce files such as WSDLs
-s <directory> specify where to place generated source files
-verbose output messages about what the compiler is doing
-version print version information
-wsdl[:protocol] generate a WSDL file. The protocol is optional.
Valid protocols are [soap1.1, Xsoap1.2],
the default is soap1.1.
The non stanadard protocols [Xsoap1.2]
can only be used in conjunction with the
-extension option.
-inlineSchemas inline schemas in the generated wsdl. Must be
used in conjunction with the -wsdl option.
-servicename <name> specify the Service name to use in the generated WSDL
Used in conjunction with the -wsdl option.
-portname <name> specify the Port name to use in the generated WSDL
Used in conjunction with the -wsdl option.
Examples:
wsgen -cp . example.Stock
wsgen -cp . example.Stock -wsdl -servicename {http://mynamespace}MyService
C:\>
Resource folder
Create the resource folder under src located in \JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
Right click on the src
New
Folder
Name of Folder: resource
Finish
Change the path to your current project directory.
C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
- src
- resource
- JRE
Project directory look like:
C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>dir
Volume in drive C is ISO1
Volume Serial Number is 2AB1-ADF2
Directory of C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach
14/02 09:47 <DIR> .
14/02 09:47 <DIR> ..
14/02 09:47 303 .classpath
14/02 09:44 473 .project
14/02 09:44 <DIR> .settings
14/02 10:20 <DIR> class
14/02 10:20 <DIR> src
2 File(s) 776 bytes
Now generate the jax-ws artificate with wsgen utility
C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>wsgen -keep -classpath class -cp class -d src -r src\resource -verbose -wsdl com.atozexamples.AToZExamplesService
warning: The apt tool and its associated API are planned to be
removed in the next major JDK release. These features have been
superseded by javac and the standardized annotation processing API,
javax.annotation.processing and javax.lang.model. Users are
recommended to migrate to the annotation processing features of
javac; see the javac man page for more information.
Note: ap round: 1
[ProcessedMethods Class: com.atozexamples.AToZExamplesService]
[should process method: printGreeting hasWebMethods: true ]
[endpointReferencesInterface: false]
[declaring class has WebSevice: true]
[returning: true]
[WrapperGen - method: printGreeting()]
[method.getDeclaringType(): com.atozexamples.AToZExamplesService]
[requestWrapper: com.atozexamples.jaxws.PrintGreeting]
[ProcessedMethods Class: java.lang.Object]
com\atozexamples\jaxws\PrintGreeting.java
com\atozexamples\jaxws\PrintGreetingResponse.java
Note: ap round: 2
C:\workspace\JAX-WS Java API XML Web Services with JDK1.6 + wsgen Utility Code Examples by Code First Approach>
Services class and artifices file generate with help of wsgen utility.
PrintGreeting.java
package com.atozexamples.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 = "printGreeting", namespace = "http://www.atozexamples.com/AToZExamplesService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreeting", namespace = "http://www.atozexamples.com/AToZExamplesService")
public class PrintGreeting {
}
PrintGreetingResponse.java
package com.atozexamples.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 = "printGreetingResponse", namespace = "http://www.atozexamples.com/AToZExamplesService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "printGreetingResponse", namespace = "http://www.atozexamples.com/AToZExamplesService")
public class PrintGreetingResponse {
@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;
}
}
Expose your service
For publish our service class to any container. In JDK itself we have EndPoint class with publish method.
We have to do new PublishAToZExamplesService
Right click on the src folder under current project.
New
Class
Type the name of class: PublishAToZExamplesService
Type the package name: com.atozexamples.publish
Check the: public static void main(String args[])
Click the Finish
package
com.atozexamples.publish;
import
javax.xml.ws.Endpoint;
import
com.atozexamples.AToZExamplesService;
public class
PublishAToZExamplesService {
public
PublishAToZExamplesService() {
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8088/AToZExamplesService",
new
AToZExamplesService());
}
}
Run the PublishAToZExamplesService file
Right click on the PublishAToZExamplesService file
Run as
Java applicaiton
Verify the service
Open the IE or any browser
Enter the URL as http://localhost:8088/AToZExamplesService
Click on the WSDL link http://localhost:8088/AToZExamplesService?wsdl
This XML file does not appear to have any style information associated with it. The document tree is shown below.
No comments:
Post a Comment