user379151
user379151

Reputation: 1379

Jersey REST request object change

My client and server use the following request object for POST :

class Person{

    String name = "";

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

}

Now my server's request object has changed to include age -

class Person{
    String name = "";
    int age = 0;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setAge(int age){
        this.age = age;
    }

    public int getAge(){
        return age;
    }
}

Similarly, server's response object also includes age now.

The old client is not compatible with the new server, throws a 400 bad request because of InvocationTargetException.

Is there a way the server can accept old requests and set null fields to default values ?

Upvotes: 0

Views: 461

Answers (1)

fmucar
fmucar

Reputation: 14548

If you are using json, you can set the json framework to ignore unkown fields

on the client side:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)

OR

// jackson 1.9 and before
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// or jackson 2.0
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

on the server side

jackson 1.x
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

OR

jackson 2.x
    @JsonInclude(Include.NON_NULL)

Upvotes: 3

Related Questions