Jacob
Jacob

Reputation: 14741

RESTful Webservices Integration With Spring and Hibernate

I am in the process of learning RESTful Webservice.

I am using Hibernate 4 and Spring 3. I would like to know if I need to implement REST services, I call the service implementation class EmployeeServiceImpl from MyController class? And invoke get, put delete methods of REST from client layers?

If Spring is used which REST project can be used CXF or Jersey? What is the best approach in creating Controller class for REST services?

Service Implementation class

@Transactional
@Service(value = "empService")
public class EmployeeServiceImpl implements EmployeeService {

@Inject
EmployeeDAO employeeDAO;

@Override
public List<Emp> findAllEmployees() {
return getEmployeeDAO().findAllEmployees();
}

DAO Implementation class

@Repository("empDAO")
public class EmployeeDAOImpl extends GenericDAOImpl<Emp> implements EmployeeDAO {

@PersistenceContext
private EntityManager entityManager;

@Override
public List<Emp> findAllEmployees() {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(Emp.class));
return getEntityManager().createQuery(cq).getResultList();
}

Upvotes: 0

Views: 8698

Answers (1)

Jean-R&#233;my Revy
Jean-R&#233;my Revy

Reputation: 5677

JAX-RS implementations (RESTeasy, Jersey) are fully J2E, that means you don't need Spring to work with. But you can use it any way.

Here a simple example of a service, decorated with the right annotations, and then don't need any Controller :

package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Jersey say : " + msg;

        return Response.status(200).entity(output).build();

    }

}

Source : http://www.vogella.com/articles/REST/article.html

Indeed, the magic inside this JSR (331) is that you don't need Controllers, it will map automatically calls from

/context/{defined_sub_context}/{entity}/{params}

to

/org.corp.package.app.services.class

if class is decorated with @Path("{entity}) and a method @Path("/{param}")

Great, isn't it ?

Upvotes: 1

Related Questions