0

I am trying to build a simple REST API with Jersey and Hibernate. The database that I am using is Neo4j.

Whenever I try to do a simple GET request, I get 500 error and I am not sure what I am doing wrong.

My project structure is like this:

enter image description here

Here is my Person class:

package com.topjavatutorial.dao;

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

@Entity
@Table(name="Person")
public class Person {

    @Id @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="gender")
    private String gender;

    @Column(name="dob")
    private String dob;

    @Column(name="firstname")
    private String firstname;

    @Column(name="middlename")
    private String middlename;

    @Column(name="lastname")
    private String lastname;


    // getters and setters
    public int getId() {
        return id;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getMiddlename() {
        return middlename;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

My PersonDAO class looks like this:

package com.topjavatutorial.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.hibernate.HibernateException;

public class PersonDAO {

  public Person getPerson(Integer id){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Person person = new Person();
      try{

          String query = "MATCH (p:Person {id:" + id + "}) "
                + "RETURN {id: p.id, "
                + "firstname: p.firstname, "
                + "middlename: p.middlename, "
                + "lastname: p.lastname, "
                + "dob: p.dob, "
                + "gender: p.gender}";

         person = (Person) em.createNativeQuery(query).getResultList();

         em.flush();
         tx.commit();
         em.clear();
         em.close();
         emf.close();

      }
      catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      } 
      return person;
   }
}

and the MyResource class is like this:

package com.topjavatutorial;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.topjavatutorial.dao.Person;
import com.topjavatutorial.dao.PersonDAO;

@Path("/people")
public class MyResource {

    @GET
    @Produces("application/json")
    public Person getPeople() {
        PersonDAO person = new PersonDAO();
        return person.getPerson(6);
    }
}

So, whenever I run the application with the following url: http://localhost:8080/Neo4jAPI/, I don't get any errors, but whenever I do http://localhost:8080/Neo4jAPI/webapi/people, I get the 500 error.

Porjaz
  • 771
  • 1
  • 8
  • 28

1 Answers1

0

You are getting "404 resource not found", this means that the requested URL did not successfully resolve to any target, as a result your getPeople() would never have been called (unless your code specifically responds with a 404 in exception handling).

I do see an issue with your getPeople()' method, it returns thePersonDAOafter callingperson.getPerson(6);` I believe that you mean for the method to be:

@GET
@Produces("application/json")
public Person getPeople() {
    PersonDAO person = new PersonDAO();
    return person.getPerson(6);
}

And there is an issue with your DAO too, your getPerson does not return a person, it just loads one up and does nothing with it, so it will get collected by the garbage collection when it falls out of scope. you should have something like:

public class PersonDAO {

public Person getPerson(Integer id) {
    .....

My recommendation is to test this in two parts, the json interface, (start by returning a hard-coded value) and the DAO (try writing a unit test for your method, it will speed up finding errors)

MartinByers
  • 1,240
  • 1
  • 9
  • 15
  • Thank you for the reply. I believe the url should be: `http://localhost:8080/Neo4jAPI/webapi/people` and with that url I am getting 500 error: `javax.persistence.PersistenceException: [PersistenceUnit: persistence] Unable to build Hibernate SessionFactory`. I have changed the code like you said. I will update my question with the new code now – Porjaz Sep 26 '17 at 11:24
  • A 500 Internal server error is the default when something fails server side, so I strongly suspect you have an exception being thrown, the stack trace will help you find the answer. – MartinByers Sep 26 '17 at 12:55