thisguy
thisguy

Reputation: 63

Using Jersey how can I have multiple setters for a field

So I am using Jersey to deserialize some input sent to an API. Initially, a particular field was a Number value now it's a String value. I was wondering if there was a way to have a multiple setters for that field since I don't want to break people who are already using the API with the old format. Thank you ahead of time.

Example of what I'm trying to do:

public class MyInput {
  private String clientId;

  public String getClientId() {return clientId;}
  public void setClient(Number clientId) {this.clientId = Long.toString(clientId.longValue());}
  public void setClient(String clientId) {this.clientId = clientId;}
}

Upvotes: 1

Views: 234

Answers (1)

rmlan
rmlan

Reputation: 4657

Since you have mentioned that you are using Jackson as your databinding library, you can annotate the setter that you would like Jackson to use (while preserving the old one).

public class MyInput {
    private String clientId;

    public String getClientId() {return clientId;}
    public void setClient(Number clientId) {this.clientId = Long.toString(clientId.longValue());}

    @JsonSetter("clientId")
    public void setClient(String clientId) {this.clientId = clientId;}
}

Upvotes: 1

Related Questions