Reputation: 5108
So, I got into this new Spring Boot project which was already under developement and while writing API's I used Enum
for @RequestParam
in my controller and it worked.
I did not write any converters for this.
Later on I noticed that in this project the other developers had written custom Converter's for this.
So I decided to search the web regarding this and all solutions that came up for using Enum
with Controller
in Spring Boot used converter, could not find any examples without converter like how I did.
Below is one an example of how I wrote this, LoanStatus
is an Enum
:
@RequestMapping(value = "/loans", method = RequestMethod.GET)
public ResponseEntity<?> getPatientsLoan(HttpServletRequest request,
@RequestParam(value = "loanStatus", required = false) LoanStatus loanStatus) {}
So is this a relatively new feature that Spring Boot accepts Enums
now without the need for converter's and that is why all the examples used converters or will I face some issue in feature cause I did not user converter's even though it is currently working for me?
Upvotes: 4
Views: 6921
Reputation: 125212
Spring has supported String
to Enum
conversion since Spring 3.0. There is a ConverterFactory
which dynamically creates a converter for the specific enum.
Prior to that you would need to write a custom Converter
or PropertyEditor
to convert enums. But basicallly with the current versions you don't need to if the String
matches the Enum
name.
If you want custom enum conversion (by some internal value or whatever) you still would need a custom converter.
Upvotes: 4