Monday, 21 March 2016

Spring+Apache CXF+Hibernate Integration java

 Create a Dynamic Web  project in eclipse . Project structure looks like below

Web.xml
  

  ApacheCXF-Up-Down-Text-File
  
    CXFServlet
    org.apache.cxf.transport.servlet.CXFServlet
    1
  
  
    CXFServlet
    /services/*
  
  
    contextConfigLocation
    WEB-INF/apache-cxf-services.xml,WEB-INF/ApplicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    index.html
  

ApplicationContext.xml


 

    
 
   
    
 
   
    
 
 
    
        
    
 
 
    
        
        
              
                 com.helloworld.model.HelloWorldDTO    
            
        
        
            
                org.hibernate.dialect.MySQLDialect
                update
                true
            
        
    
 
    
    
        
        
        
        
    

apache-cxf-services.xml


 
 
    
 
   
    
     
   
    
        
            
         
        
            
        
        
            
        
       
   
  
    


HelloWorldDTO.java

package com.helloworld.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.cglib.beans.BeanCopier.Generator;

@Entity
@Table(name="helloworld")
public class HelloWorldDTO {
 @Id
 @GeneratedValue
 @Column(name="id")
    private int id;
 
 @Column(name="name")
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

}
IHelloWorldService.java

package com.helloworld.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.helloworld.model.HelloWorldDTO;

@Path("/helloservice")
public interface IHelloWorldService {
 
 @GET
 @Path("sayhello/{name}")
 @Produces(MediaType.APPLICATION_JSON)
 public HelloWorldDTO sayHello(@PathParam("name") String name);

}
HelloWorldServiceImpl.java

package com.helloworld.service;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.helloworld.model.HelloWorldDTO;

@Transactional
@Service("helloService")
public class HelloWorldServiceImpl implements IHelloWorldService{
 @Autowired
 private SessionFactory sessionFactory;

 public SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

 @Override
 public HelloWorldDTO sayHello(String name) {
  HelloWorldDTO hdto=new HelloWorldDTO();
  hdto.setName(name);
  sessionFactory.getCurrentSession().save(hdto);
  
  return hdto;
 }

}

Please enter the below URL in broswer. The Out put json looks like
You can see the records inserted in database as below