1

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. Here is my Java class:

public class RequestClass {

    String email_id;
    String password;

    public String getEmailId() {
        return email_id;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString(){

        return email_id+" "+password;
    }
}

Here is the web service code:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/dbconnect3")
public String connectToDbTest3(RequestClass rc) {
    System.out.println("connectToDbTest3");
    String email_id = rc.getEmailId();
    String password = rc.getPassword();
    System.out.println(email_id + " " + password);
}

This throws exception UnrecognizedPropertyException with message "Unrecognized field "email_id" (Class jaxrs.RequestClass), not marked as ignorable".

In case i am not using the annotation @JsonIgnoreProperties(ignoreUnknown = true) in my Java class, the output I am getting on line 09 is:

null myPassword

So I don't want to ignore Unrecognized field instead I want to get the value of email_id.

Please tell why It shows email_id as Unrecognized field while password is fetched successfully.

4

1 回答 1

2

只需在下面给出@JsonProperty("email_id")的之前添加getEmailId()

@JsonProperty("email_id")
public String getEmailId() {
   return email_id;
}
于 2013-03-01T10:59:53.900 回答