Web Services Java Tutorial
Last Updated on Tuesday, 30 October 2012 16:17
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
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 :
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.)
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 :
No JAX-WS context information available.
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 aService 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
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 :
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
Service Endpoint interface
Used only by JAX-WS clients
JAXB XML Registry
|Reverse||Wrapper bean for request message|
|ReverseResponse||Wrapper bean for response message|
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.
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.
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
Tags: project, artifactid, http, class, version, create, client, name=, service, reverse
Sébastien Dante Ursini
17 Years of experience in Java
22 Year in Banking/Finance
Based in Geneva/Switzerland