0

How can I obtain the enum corresponding value from a given String in camel case format? For instance, for the following enum:

public enum Status {
  NEVER_DONE, DONE
}

and this controller:

public interface QuestionsController {
  @GetMapping(value = "/questions", produces = MediaType.APPLICATION_JSON_VALUE)
  List<Question> getQuestions(@RequestParam String status);
}

How could I map the received String to the corresponding value from Status so these requirements are met:

  • NEVER_DONE and neverDone map to NEVER_DONE.
  • DONE and done map to DONE.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
thmasker
  • 406
  • 1
  • 9
  • 21
  • 1
    Most probably you are looking for [this](https://stackoverflow.com/a/4198066/3981539) – Kavitha Karunakaran Feb 17 '21 at 16:55
  • but I have to call explicitly `find` within `getQuestions`. Is there a way to make Spring automatically make this mapping before doing anything else in the controller? – thmasker Feb 17 '21 at 17:21
  • 1
    I do not think that would be possible with Spring at the moment. From design perspective it is always good to do thorough validation of incoming request body so that injection based attacks won't happen. And the validation logic is best written by the application developer himself than the framework developer. – Kavitha Karunakaran Feb 17 '21 at 17:55

0 Answers0