0

I have pojo class Ticket

public class Ticket implements Serializable{

  @JsonProperty("lineItemStatus")
  private String revisionStatus;
  public String getRevisionStatus() {
    return revisionStatus;
  }
  
  public void setRevisionStatus(String revisionStatus) {
    this.revisionStatus = revisionStatus; 
  }

  public void setRevisionStatus(RevisionStatus revisionStatus) {
    String status = "";
    if (revisionStatus != null) {
      switch (revisionStatus) {
      case added: {
        status = "New";
        break;
      }
      case updated: {
        status = "Modified";
        break;
      }
      }
    }
    this.revisionStatus = status;
  }
}

Also I have an enum

public enum RevisionStatus {
    added {
      @Override
      public String getName() {
        return this.name();
      }
    },
    updated {
      @Override
      public String getName() {
        return this.name();
      }
    }
    public abstract String getName();
  }

During GET request , I use setRevisionStatus(RevisionStatus revisionStatus) method and i get response like for example {"lineItemStatus": "Modified"} which is fine But problem occurs during PUT request . During PUT request, my requirement is that I should be able to send payloads like for {"lineItemStatus": "Modified"} or {"lineItemStatus": "New"} or {"lineItemStatus": "abc"} , meaning lineItemStatus should be able to accept any String value . I am using @RequestBody Ticket ticket for receiving the payload. The debugger doesn't come inside the controller and fails at the payload step . How do I handle this error without making any changes inside Enum ?

1
  • Add @JsonProperty("added") etc to your enum Commented Sep 22, 2021 at 18:47

1 Answer 1

1

You can do it by updating your enum:

public enum RevisionStatus {
  added("New") {

    @Override
    public String getName() {
      return this.name();
    }
  },
  updated("Modified") {

    @Override
    public String getName() {
      return this.name();
    }
  };

  private String status;

  private RevisionStatus(String status) {
    this.status = status;
  }

  public abstract String getName();

  /**
   * @return the status
   */
  public String getStatus() {
    return status;
  }
  
  @JsonCreator
  public static RevisionStatus fromValue(String text) {
    if(StringUtils.isNoneBlank(text)) {
      for (RevisionStatus b : RevisionStatus.values()) {
        if (String.valueOf(b.toString()).equalsIgnoreCase(text.trim())) {
          return b;
        }
      }
    }
    return null;
  }
}

Here we have updated enum to value enum. And fromValue() method with @JsonCreator annotation will bind this String value with the Enum constant.

Additionally this way you won't need that switch cases during get method. As getStatus() method will give you the status value directly. And that will improve your code design also.

Just an additional point here, I don't see any need of this getName() method, as the placed where getName() is called, directly name() can be called. As that is also a public method.

Sign up to request clarification or add additional context in comments.

3 Comments

Can this be done without touching the enum. The regression scope is higher and I am not allowed to touch the enum
I don't see any regression impact of this change in enum. As this is added functionality. But still if you want to avoid it, another solution would be holding the value in JSONString. And then inside getter of the parent POJO do the transformation to enum. This thing will increase technical debt in your code.
Sorry you can hold that in normal String also. No need of JSON String. And update getter or POJO holding this enum to create enum from String (or some another method)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.