Monday, July 22, 2013

Simple JAX-WS Web Service (SOAP based) for Beginners


Today, i am going to explain simple JAX-WS web service( based on SOAP) for the beginners who are new to web service development. 
Web Services can be divided into two categories.
1. SOAP based web services
2. RESTful web services.

JAX-WS supports for both SOAP based and RESTful web services. In this post, we are going to discuss about the SOAP based web services. AS an entry point for the SOAP web service, first we will discuss the architecture of the typical SOAP based web service.


 


Here you can see that the SOAP client will communicate with the SOAP service with the aid of the SOAP libraries. SOAP libraries will be responsible to send and receive request and response between client and server. Here the both request and response will be the SOAP documents and those will be exchanged between client underlying SOAP libraries and the Service underlying SOAP libraries. These SOAP libraries will do the marshalling and unmarshaling whenever required.

What is marshalling and unmarshalling?

Marshalling – converting Java objects into the XML files.
Unmarshalling - converting XML files into the Java objects.

JAX-WS uses JAXB (Java Architecture for XML Binding) for the marshalling and unmarshalling.


In this example, we are going to look at the simple SOAP based 
web service that prints the hello concatenating with the provided 
web service input parameter as the output. Refer the below project 
tree structure to identify the components associated with the 
web service.


 
I will briefly explain the web service concepts by getting the
 examples from above sample web service.
  • Service Endpoint Interface (SEI) – HelloWorld.java 
  • Service Implementation Bean (SIB) – HelloWorldImpl.java
  • Web Service Publisher - HelloWorldPublisher.java 
Then we need to look at SEI, SIB and WsPublisher in detail.


SEI : - Service  Endpoint Interface
first of all you need to know what is the SEI. SEI is just a Java interface
that defines/declares all the web service methods. In this example,
the following file will be the SEI.

HelloWorld.java
package com.jax.ws.samples;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * <p>
 *     this will be the SEI that declares all the web service methods
 * </p>
 */
@WebService
public interface HelloWorld {

    @WebMethod
    String printWelcomeMessage(String username);
} 
SIB :- Service Implementation Bean SIB is the implementation of SEI. That means in the SIB, all the 
web methods declared in the SEI will be implemented.
SIB can be implemented as two ways.
 
 1. POJO class
2. Stateless Session EJB

In this example will be using POJO class as SIB to implement SEI as follows.

HelloWorldImpl.java

package com.jax.ws.samples;

import javax.jws.WebService;

/**
 * <p>
 *     this will be the SIB that implements all the web service methods
 *     declared in the SEI.
 * </p>
 */
@WebService(endpointInterface = "com.jax.ws.samples.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Override
    public String printWelcomeMessage(String username) {
        return "Hello "+username;
    }
}
as you can see, the end point interface will be the relevant java 
interface (SEI) that is implemented by the relevant SIB.


Publishing the Web Service 
once the SEI and SIB have been completed  and compiled, the web 
service is ready to be published. In production mode,web service 
should be published using the application servers such as Tomcat, 
JBoss, WebLogic etc... but for the testing purpose it is possible to 
publish the web service with the simple java program as follows.

HelloWorldPublisher.java

package com.jax.ws.samples;

import javax.xml.ws.Endpoint;
/**
 * <p>
 *     this will be the sample web service publisher class
 *     whose responsibility to publish the given web service
 * </p>
 */
public class HelloWorldPublisher {

    public static void main(String []args){
        //1st argument - web service publication URL
        //2nd argument - instance of SIB
        Endpoint.publish("http://localhost:6666/sayHello",new HelloWorldImpl());
    }
}

you can publish the web service by running this simple program (HelloWorldPublisher.java). After running the publisher program, it is time to check whether the 
published web service is up and running. This can be done with web 
browser by getting the related WSDL document as follows.
 
http://localhost:9999/sayHello?wsdl


I think this will be helpful for you to start your first Java web service development.

This sample web service is available in the following GitHub repository. 
https://github.com/chathurangat/jax-ws-helloworld
 
I think this will give you an brief idea about how to develop web services with java. 
Hope this will helpful for you
 
Thanks and Regards
Chathuranga Tennakoon
 

Wednesday, July 17, 2013

Difference between Unidirectional and Bidirectional mapping in hibernate


Unidirectional Mapping

When only one of the pair of entities contains a reference to the other, the association is unidirectional. for example, assume that there are two entities called Lecturer and Course.

In unidirectional mapping, the lecturer will hold a reference for the course OR course will hold a reference for the lecturer.(Not Both) It is mandatory that the references should not be mapped to the both directions and there should be only one direction(unidirectional) mapping.

In unidirectional mapping, it will provide the navigational access only to one direction.

eg:-

assumption: lecturer can teach only once course and a given course can be taught only by one lecturer

unidirectional mapping  from lecturer to course

class Lecturer{

   private Long id;
   private  String lecturerName;
   private Course;

   //getter and setters
}


class Course{
 
   private Long id;
   private String courseName;

    //getter and setters
}


unidirectional mapping  from course to lecturer


class Lecturer{

   private Long id;
   private  String lecturerName;


   //getter and setters
}


class Course{
 
   private Long id;
   private String courseName;
   private Lecturer lecturer;

    //getter and setters
}



Bidirectional Mapping

if the association between both entities are mutual, then it is known as bidirectional mapping. in bidirectional mapping, the lecturer should hold to a reference for the course and the same time the course should a reference to the lecturer.

therefore in bi-directional mapping, it will provide the navigational access to the both directions.

eg:-
assumption: lecturer can teach only once course and a given course can be taught only by one lecturer

class Lecturer{

   private Long id;
   private  String lecturerName;
   private Course;

   //getter and setters
}


class Course{
 
   private Long id;
   private String courseName;
   private Lecturer lecturer;

    //getter and setters
}


hope this will help for you!

Cheers
Chathuranga Tennakoon
www.chathurangaonline.com