Java – How to integrate hibernate and REST web service in java

hibernatejavarestweb services

I am going to develop which will be web application as well as mobile (android / iOS / windows) application. The database in this application will be managed by Hibernate. Also as it is cross platform application, web service will also be used. What I know so far is:

HIBERNATE:

  • POJO Files (the getter-setter ones which will create database tables)
  • Model (the java class which will interact with database)
  • Controller (basically servlet which will get data from view [jsp],
    set it in POJO object and pass this object to Model for any of CRUD
    operation)
  • View (the jsp pages)

REST WEB SERVICE:

  • Web service implementation class, which have web methods, which can
    be called by URL from client, and it can return JSON or XML format
    data.

So now my question is:

  • How to integrate these both? Should I put my all POJOs and Model
    files to web service? If no, than what to do in this situation? If
    yes, than how to do that (simple example)?

Best Answer

You should take a look at the JAX-RS spec and do a tutorial. I like running Jersey but there are many implementations. Do not reinvent the wheel and try to build a RESTful interface using hand-rolled servlets. This will make building your REST interface a snap. Integrating to Hibernate is just writing the code to do your queries. The JAX-RS spec allows you to register MessageBodyReaders and MessageBody writers which do the translation between your objects and any serialized formats you wish to support. There are various tools for translating to and from JSON. GSON is a solid choice but there are others.