0

The request model of the REST API has one enum field:

public enum CommentContext { STUDENT_FEEDBACK, STUDENT_QUESTION; } 

Now I want to remove the STUDENT_ prefix from the enum value, but without breaking existing callers of the API.

I tried to use @JsonAlias, like this:

public enum CommentContext { @JsonAlias ("{FEEDBACK, STUDENT_FEEDBACK}") FEEDBACK, @JsonAlias ("{QUESTION, STUDENT_QUESTION}") COMMENT; } 

But the API is failing with 400 Bad Request, when STUDENT_FEEDBACK is passed as the value of that enum field in the request JSON.

Is it possible to deserialize this CommentContext object from JSON for either of the alternative values of this enum field such as FEEDBACK or STUDENT_FEEDBACK?

1

1 Answer 1

0

You can do it using @JsonCreator. Here i gave solution for your problem:

public enum CommentContext { STUDENT_FEEDBACK, STUDENT_QUESTION; @JsonCreator public static CommentContext setValue(String key){ Optional<CommentContext> commentContext = Arrays.stream(CommentContext.values()) .parallel() .filter(ct -> ct.toString().equals(key) || ct.toString().substring(8).equals(key)) .findAny(); return commentContext.orElse(null); } } 

you can also see this link

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.