I am using Spring Boot to write a web service. I have an entity Animal
and an enum Kind
that can be either Kind.DOG
or Kind.CAT
. The Animal
class contains private Kind kind
as one of its instance variables. When I make an HTTP request, I pass in a string value for kind
and when the request maps to the header:
@RequestMapping(method=RequestMethod.POST, value="/create")
public ResponseEntity<Animal> createAnimal(@RequestBody Animal animal)
I want kind
to be internally converted to Kind.DOG
/Kind.CAT
if I am passed in either "dog" or "cat". Right now, if I pass in DOG
or CAT
, it works fine but if's lowercase it doesn't. Can someone tell me what to do? I tried the following:
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(Kind.class, new KindEnumConverter());
}
@RequestMapping(method=RequestMethod.POST, value="/create")
public ResponseEntity<Animal> createAnimal(@RequestBody Animal animal)
and
public class KindEnumConverter extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(Kind.valueOf(text.toUpperCase()));
}
}
but it didn't work.