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;
}
}