@POST w web service

0

czesc, pomóżcie proszę bo zwariuje .. przesiedziałem dziś z 6h i nie wiem co sie dzieje.
A więc tak robie web service wszystkie inne metody z @GET są spoko dzis postanowiłem nieco rozszeczyć API i dodaj funkcje z @POST wydaje się być ok ale wbijam JSONA w WizToola pokazuje mi się HttpStatus 200 czyli ok, odpalam pgAdmina a tam nie ma rekordu spoglądam do Tomcat'a a tam log : Illegal Argument Exception : attempt to create saveorUpdate event with null entity.
Pomóżcie proszę !
Mój service:

@Path("/message")
public class MessageRestService {
    StudentDao dao = new StudentDao();


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

        try {
            Student s = new Student();
            s.setImie(msg);
            dao.addStudent(s);

            String result = "Dodano studenta o imieniu : " + msg;
            return Response.status(200).entity(result).build();

        } catch (Exception ex) {

            ex.printStackTrace();
            String fail = "Restful example : " +ex;
            return Response.status(404).entity(fail).build();
        }


    }
    @GET
    @Path("get/{idStudent}")
    public Response getStudentById (@PathParam("idStudent") int id)
    {
        try
        {
            String result = "Wybrano studenta" + dao.getStudentById(id);
            return Response.status(200).entity(result).build();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            String fail = "Nie ma takiego studenta!"+ex;
            return Response.status(404).entity(fail).build();
        }
    }

    @POST
    @Consumes({"application/xml", "application/json"})
    @Produces ("application/json")
    @Path("/addStd")
    public StudentResponse addStudent (StudentRequest request)
    {
        StudentResponse response = new StudentResponse();

        try
        {
            dao.addStudent(request.getStudent());
        }
        catch (Exception ex)
        {
            response.setSucces(false);
            response.setErrorMsg(ex.getMessage());
        }
        return response;
    }
}

i Dao

public class StudentDao
    {
        List listStudent = new ArrayList();


        public void addStudent(Student student)  {
            Transaction trns = null;
            Session session = HibernateUtil.getSessionFactory().openSession();

            try {
                trns = session.beginTransaction();
                session.save(student);
                session.getTransaction().commit();

            } catch (RuntimeException e)
            {
                if (trns != null) {
                    trns.rollback();
                }
                e.printStackTrace();
            } finally {
                session.flush();
                session.close();
            }
        }

        public void updateUser(Student student) throws Exception {
            Transaction trns = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                trns = session.beginTransaction();
                session.update(student);
                session.getTransaction().commit();
            } catch (RuntimeException e) {
                if (trns != null) {
                    trns.rollback();
                }
                e.printStackTrace();
                throw new Exception(" -UpDate Test");
            } finally {
                session.flush();
                session.close();
            }
        }

        public void deleteStudent(int id) {
            Transaction trns = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                trns = session.beginTransaction();
                Student student = (Student) session.load(Student.class, new Integer(id));
                session.delete(student);
                session.getTransaction().commit();
            } catch (RuntimeException e) {
                if (trns != null) {
                    trns.rollback();
                }
                e.printStackTrace();
            } finally {
                session.flush();
                session.close();
            }
        }

        public Student getStudentById (int id) throws Exception {
            Student student = null;
            Transaction trns = null;
            Session session = HibernateUtil.getSessionFactory().openSession();
            try {
                trns = session.beginTransaction();
                String queryString = "from Student where id = :id";
                Query query = session.createQuery(queryString);
                query.setInteger("id", id);
                student = (Student) query.uniqueResult();

            } catch (RuntimeException e) {
                e.printStackTrace();
                throw new Exception("ById");
            } finally {
                session.flush();
                session.close();
            }
            return student;
        }


    }
1

Próbujesz zapisać nullową encję. Na pewno dobrze wysyłasz posta? W sensie poprawnie tworzysz tego requesta?

0

Wydaje mi się że tak, tworze go w JSON i wygląda to tak :
{
"imie" : "TEST"
}
Kiedy określałem encję wywalało mi bład również ?

1

Postaw breakpoint w metodzie POST i sprawdź co jest w requeście..
Lub postaw breakpoint w pierwszej linijce AddStudent w DAO i sprawdź jakią wartość ma obiekt student (zapewne null - w tym przypadku wysyłasz złego jsona)

0

Sprawa rozwiązana, rzeczywiście błędem był JSON - zła konstrukcja.
Prawidłowa to (dla innego entity):
{
"nauczyciel": [
{
"nazwisko" : "TEST"
}
]
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1