I have a problem with enum as @RequestParam
. In the method getRooms
there are two enums: Sort.Direction
and SourceFunding
. The Sort.Direction
is the build-in Spring enum, and it convert from String to Enum perfectly:
http://address:port/all?sortDirection=ASC => OK
http://address:port/all?sortDirection=DESC => OK
but when I call
http://address:port/all?sourceFunding=PUBLIC_INSURANCE => ALWAYS ERROR.
I have read and tested:
Spring's @RequestParam with Enum
Spring boot able to accept Enum as Request parameter
Enums as Request Parameters in Spring Boot Rest
How to convert multiple values to enum in RequestParm?
@RequestParam defaultvalue does not accept enum value as a default value
Accept and parse integer for Enum type query parameter in Java
@RequestParam with a list of parameters
and many others with no effect. Also I have tried to emulate Sort.Direction in my enum.
****** the code *******
The error: { "timestamp": "2022-07-15T15:53:27.371+00:00", "status": 400, "error": "Bad Request", "message": "Failed to convert value of type 'java.lang.String' to required type 'xxx.xxxxx.Leo.booking.SourceFunding'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam xxx.xxxxx.Leo.booking.SourceFunding] for value ''PUBLIC_INSURANCE''; nested exception is java.lang.IllegalArgumentException: No enum constant xxx.xxxxx.Leo.booking.SourceFunding.'PUBLIC_INSURANCE'", "path": "/room/all" }
and also could be
Parameter value [PRIVATE_INSURANCE] did not match expected type [xxx.xxxxx.Leo.booking.SourceFunding (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [PRIVATE_INSURANCE] did not match expected type [xxx.xxxxx.Leo.booking.SourceFunding (n/a)]
The controller:
@GetMapping(path = "/all")
public Page<Room> getRooms(@RequestParam(defaultValue = "0") Integer pageNumber,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(defaultValue = "id") String sortBy,
@RequestParam(defaultValue = "ASC") Sort.Direction sortDirection,
@RequestParam(required = false) Long departmentId,
@RequestParam(required = false) Long hospitalId,
@RequestParam(required = false) SourceFunding sourceFunding
) {...}
The enum:
public enum SourceFunding {
PUBLIC_INSURANCE,
PRIVATE_INSURANCE,
PAID_BY_CITIZENS,
PAID_BY_COMPANY,
CHARITY,
COLLECTIVE_FUNDING_FSS,
OTHER,
NONE;
}
update 1 - that's crazy...
SourceFunding.valueOf(sourceFundingString); => Error 'No enum constant ...'
log.info("!! {}", Arrays.toString(SourceFunding.values())); => Return all enum's fields