I am trying to build simple REST api having simple endpoints doing CRUD operations. I am JAX-RS implementation jersey here. For DB part I want to connect to the in-memory database h2.
When I hit the endpoint I get empty body, with status 200 ok as I have set in response, but body doesn't show anything. I don't see any errors in the console after hitting the endpoint.
This is my Dao class
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.shrikant.jersey_restapp.model.Employee;
public class EmployeeDao {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("my-persistence-unit");
public void saveEmployee(Employee employee) {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
em.close();
}
public Employee findEmployeeById(int id) {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Employee emp = em.find(Employee.class, id);
em.getTransaction().commit();
em.close();
return emp;
}
public List<Employee> getAll() {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
List<Employee> list = em.createQuery("from Employee").getResultList();
em.getTransaction().commit();
em.close();
return list;
}
}
and this is my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="my-persistence-unit" transaction-type="RESOURCE_LOCAL">
<class>com.shrikant.jersey_restapp.model.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:demo" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties>
</persistence-unit>
</persistence>
service
@Path("/company")
public class EmployeeController {
private EmployeeService employeeService = new EmployeeService();
@GET
@Path("/empall")
public Response getAllEmployee() {
return Response.status(200).entity(employeeService.findAll()).build();
}
@GET
@Path("/emp/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getEmployee(@PathParam("id") int id) {
Employee emp = employeeService.getEmployeeById(id);
return Response.status(200).entity(emp).build();
}
@POST
@Path("/emp")
@Consumes(MediaType.APPLICATION_JSON)
public Response setEmployee(Employee employee) {
employeeService.addEmployee(employee);
return Response.status(200).entity("Entity saved").build();
}
}
As you can see I have also set the show_sql property to true but I don't see any sql in the console either.
I might be missing something here, because I am not sure if h2 will start automatically. But I have also started h2 using h2.**.jar and connected to it, and still same response. Using first h2 with jersey
Don't understand what's going wrong, does anyone know what I am missing or what's going wrong ??
Thanks