Spring @RequestBody and Enum value

前端 未结 3 1054
执念已碎
执念已碎 2020-12-08 21:30

I Have this enum

public enum Reos {

    VALUE1(\"A\"),VALUE2(\"B\"); 

    private String text;

    Reos(String text){this.text = text;}

         


        
3条回答
  •  独厮守ぢ
    2020-12-08 21:58

    I've found what I need. http://chrisjordan.ca/post/50865405944/custom-json-serialization-for-enums-using-jackson.

    It was 2 steps.

    1. Override the toString method of the Reos enum
    @Override
    public String toString() {
        return text;
    }
    
    1. Annotate with @JsonCreator the fromText method of the Reos enum.
    @JsonCreator
    public static Reos fromText(String text)
    

    And that's all.

    I hope this could help others facing the same problem.

提交回复
热议问题