In addition to one good solution presented (factory method), there are 2 other ways:
- If 'MyEnum.toString()' would return "coins", you can make Jackson use "toString()" over "name()"with
ObjectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
- You could add some other method to return id to use, and mark that method with
@JsonValue
annotation (you can actually use that ontoString()
as well, instead of enabling above feature) -- if that annotation exists, value returned by that method is used as the id.