BobMorane
BobMorane

Reputation: 4306

Jersey 2.27 Unable to handle POST

I am new to Jersey and have been trying to implement a POST handler for a simple Student REST resource consisting of a name and a CIP, which is another string. I am using Tomcat 8.5:

public class StudentResource {

    String m_name;
    String m_cip;

    public StudentResource(String p_name, String p_cip) { 
        m_name = p_name;
        m_cip = p_cip;
    }

    public String getName() {
        return m_name;
    }

    public void setName(String p_name) {
        m_name = p_name;
    }

    public String getCip() {
        return m_cip;
    }

    public void setCip(String p_cip) {
        m_cip = p_cip;
    }
}

The POST handler:

@Path("students")
public class StudentResourceHandler {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public StudentResource onNewStudent(StudentResource p_newStudent) {
        return p_newStudent
    }
}

I only want to return what I have posted... I have been able to process JSON with no errors in my GET handler (i.e. returning JSON works), but somehow the POST handler always fail with this exception:

javax.servlet.ServletException: javax.ws.rs.ProcessingException: Error deserializing object from entity stream.

I am using RESTClient to make my requests. Here a request that fails for me:

header:

  Content-Type : application/json

body:

  {
    "cip": "gmar2104",
    "name": "Marcel"
  }

There is definitely something wrong with what I am doing, but I can't find why. Any help would be appreciated. Thanks.

Upvotes: 0

Views: 860

Answers (1)

Allan Braga
Allan Braga

Reputation: 470

If you don't have a default constructor Jackson cannot instantiante.

Try to add a default constructor:

public class StudentResource {

    String m_name;
    String m_cip;

    public StudentResource(String p_name, String p_cip) { 
        m_name = p_name;
        m_cip = p_cip;
    }

    //Default constructor 
    public StudentResource() {
    }

    public String getName() {
        return m_name;
    }

    public void setName(String p_name) {
        m_name = p_name;
    }

    public String getCip() {
        return m_cip;
    }

    public void setCip(String p_cip) {
        m_cip = p_cip;
    }
}

Upvotes: 4

Related Questions