My approach is to write a public method to convert to Map Implement it however convenient it is. In fact, I think the annotation should be placed on the get method of the enumeration instance
public class Result {
private StatusEnum status;
...
@JsonValue
public StatusEnum getStatus() {
return status;
}
public void setStatus(status) {
this.status = status;
}
}
I haven’t used this before, so I don’t know much about it, but you can try it. I couldn’t resist my curiosity and tried it. . . The one above is not feasible. . . But don’t use @JsonValue
If you just need serialization, you can use 1 and @JsonSerialize to implement it simply. The former is simple and the latter is flexible; If you need deserialization, you can use @JsonDeserialize code> and @JsonSerialize实现,前者简单,后者灵活; 如果需要反序列化,那可以用@JsonDeserialize和@JsonCreator, the former is flexible and the latter is simple :)
My approach is to write a public method to convert to Map
Implement it however convenient it is. In fact, I think the annotation should be placed on the get method of the enumeration instance
I haven’t used this before, so I don’t know much about it, but you can try it.
I couldn’t resist my curiosity and tried it. . . The one above is not feasible. . . But don’t use @JsonValue
Personal test, it works
enum
默认jackson只能序列化为索引和枚举名称,如果你需要返回{}
json object 需要自已实现Serializer
Jdk8Module 参考这个实现自己对
StatusEnum
Do the processing.You can take a look at the following points:
SerializationFeature.WRITE_ENUMS_USING_TO_STRING
;@JsonSerialize
和@JsonDeserialize
;@JsonCreator
If you just need serialization, you can use 1 and
@JsonSerialize
to implement it simply. The former is simple and the latter is flexible;If you need deserialization, you can use
@JsonDeserialize
code> and@JsonSerialize
实现,前者简单,后者灵活;如果需要反序列化,那可以用
@JsonDeserialize
和@JsonCreator
, the former is flexible and the latter is simple :)