I have a master enum class which is essentially a class definition for a type of object. For example it looks something like the example below:
public enum ColorDefinition
{
private String abbrev;
private String color;
private Class colorClass;
RED("RD", "Red", Red.class),
GREEN("GN", "Green", Green.class),
BLUE("BL", "Blue", Blue.class)....
}
I am trying to set up a post request from a Javascript model, that sends a mapping in the body such as
{Red : 255, Green : 0, Blue: 0}
To a spring controlled endpoint that uses
@RequestMapping(value = "v1/color/EnableColors", method = RequestMethod.POST)
@ResponseBody
public ResponseObject enableColors(@RequestBody Map<ColorDefinition, Integer> colorMapping)
To which I get the following error message:
Can not construct Map key of type ColorDefinition from String "Red": not a valid representation: Can not construct Map key of type ColorDefinition from String "Red": not one of values for Enum class
What am I doing wrong here? Do I need some other method in the enum class to properly convert the incoming enum value? Should it be using another value from the enum (I have tried them with no success)? Any help is appreciated, it seems like this should be possible to automatically convert the incoming values, I just can't figure it out!