Web Services Java Tutorial
|
|
|
Web Services in Java using Maven
Creating your first Web Service is incredibly easy. In fact, by using Maven you can have your first service up and running in minutes with few coding.We present a step-by-step walkthrough of building and deploying a Web service that performs a reverse string manipulation and return the result.
In this tutorial the following topics will be covered :
- Create the server side project
- Create a web service
- Publish the web services
- Create the client side project
- Import the wsdl and binding file
- Create the client class
- Run the web service client application
Step 1 : Create the server project
In this step we will create a project called wsServer (Web Service Server) using Maven . �The pom file below define the required dependencies and the project structure.
In our tutorial we will use the reference implementation of the Java API for XML Web Services (JAX-WS) specification and the corresponding JAX-WS Maven plugin .
This plugin contains Maven adapter for JAX-WS's toolset. The original version of this was developed in the codehaus mojo project, but as of March 2007, the project is moved to jax-ws-commons.
JAX-WS supports the following standards
- JAX-WS 2.0/2.1/2.2 (JSR 224)
- WS-I Basic Profile 1.2 and 2.0
- WS-I Attachments Profile 1.0
- WS-I Simple SOAP Binding Profile 1.0
- WS-Addressing 1.0 - Core, SOAP Binding, WSDL Binding
In our pom we have configured the Maven plugin to reads a service endpoint implementation class ( com.ubiteck.ws.server.MyServiceImpl ) and generates all of the portable artifacts for a JAX-WS web service. The <sei> argument in the plugin indicate �the service endpoint implementation class name.
If you are using eclipse you can generate the eclipse project setting files by using the following Maven command :
Step 2 : Create the Web service
We will now declare the SEI . A service endpoint interface (SEI) is a Java interface that declares the methods that a client can invoke on the service.
We have one unique method called reverse. As explained it returns a revesed string according to the value provided. The reverse method is tagged with the a @WebMethod annotation to mark the method as exposed as a Web Service operation .
The next source code is the implementation of the SEI. �The @WebService annotation is used to define the endpointInterface (The complete name of the service endpoint interface defining the service's abstract Web Service contract.)
Step 3 : Publish the web service
Now i want to have the WebService exposed when I just run the main method of the following class (Publisher). That is where the EndPoint class comes in:
The Endpoint publish static method specify the URL on which the WebService should be available and�pass the class-with-WebService annotation to handle the calls received at that URL.
If you run execute the class and type the url in your favorite browser you should see the following page :
Web Services
No JAX-WS context information available.
JAX-WS RI
Endpoint.publish
API uses by default a light-weight
HTTP server implementation
that is included in Sun's Java SE 6. So no, it does
not
use an embedded GlassFish nor an embedded Tomcat and even less your existing Tomcat install: it uses an
embedded
container i.e. something running inside the same JVM. Just FYI, it is however possible to plug other implementations as long as they provide a
Service Provider Implementation
(SPI). For example, Jetty 6 does so, see
J2se6HttpServerSPI
.
To obtain the wsdl content type the same url with ?wsdl at the end :
http://localhost:8080/WS/MyService?wsdl
The result should look like :
The XSD could be accessed using
http://localhost:8080/WS/MyService?xsd=1
Step 4 : Create client side project
We have now a running Web Service, we can call the Web Service using the browser we are ready to develop a client application in Java. To do that we will parse and the wsdl file and generate java code needed to to access our Web Service. To do so we will use the jaxws maven plugin .
Below the Maven project object model :
Step 5 : Import the wsdl and binding file
In the pom we have defined a
wsimport
goal using the
jaxws-maven-plugin
to import and generate the java code according to the wsdl found at
http://localhost:8080/WS/MyService?wsdl
and defined using the
The result of the wsimport should display the following information
So we have 6 java files generated
| Class | Purpose |
|---|---|
|
MyService |
Service Endpoint interface |
| MyServiceImplService |
Used only by JAX-WS clients |
|
ObjectFactory |
JAXB XML Registry |
| Reverse | Wrapper bean for request message |
| ReverseResponse | Wrapper bean for response message |
| Package-info |
Holder for JAXB package annotations |
To use the generated code in our Eclipse project we are using the build-helper-maven-plugin to add the generated code in our source folders.
Step 6 : Create the Web service client class
To ensure we have the java source generated as a source folder in our Eclipse project we will cleanup and recreate our Eclipse project settings file using the following maven command
We have now access to the jax-ws client class then we can create our web service client class :
In our class we are instantiating the MyServiceImplService class to obtain an instance of our service Endpoint interface by calling getMyServiceImplPort method.
Step 7 : Run the web service client application
The client application is configured we can call our service :
That are all the steps for publishing and using a WebService in plain old Java application.
If you enjoyed this tutorial and want to promote it don't hesitate to click on
Sébastien Dante Ursini
Java/Finance Specialist
17 Years of experience in Java
22 Year in Banking/Finance
Based in Geneva/Switzerland