I have the following class
and enum
s:
import lombok.Data;
// other imports...
@Data
public class MapTest{
private MyFirstEnum myFirstEnum;
private MySecondEnum mySecondEnum;
}
public enum MyFirstEnum{
MY_FIRST_ENUM1,
MY_FIRST_ENUM2
}
public enum MySecondEnum {
MY_SECOND_ENUM1,
MY_SECOND_ENUM2
}
and this spring controller:
@PostMapping("/testMap")
@ResponseBody
public void TestMap(@RequestBody MapTest mapTest){
}
Since an enum can be looked up by its name what I would like to do is to post a json
to the controller and that the appropriate props will be serialized by their name:
{
"myFirstEnum": "MY_FIRST_ENUM1",
"mySecondEnum": "MY_SECOND_ENUM2"
}
I've tried to set up a @JsonDeserialize
but i couldn't get the type of the enum inside the overridden function:
// what type should i use here?
public static class StringToEnum extends JsonDeserializer<???> {
// how do i get the type of the current enum?
@Override
public ??? deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
??? res = Enum.valueOf(p.getText());
return res;
}
}
Update:
I've failed to mention that i'm using lombok's @data attribute for automatically generating getters and setters, which doesn't work well with enum bindings (not sure why).
I guess that laziness comes with a price.