0

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  

Alex
  • 751
  • 1
  • 9
  • 28

1 Answers1

0

This looks to me while using postman, your query parameter input is not getting correct value. Error shows it has single quote in your input 'PUBLIC_INSURANCE'.

Spring uses Enum.valueOf(string) to evaluate queryParam value. If we use 'PUBLIC_INSURANCE' ,it will not find any corresponding enum.

Can you try to log your input request in tests section of postman via console.log(pm.request.url.query) & check if your input is proper or not.

Console log in postman can be visible by clicking View->Show Postman Console

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • from the postman concole ```GET http://localhost:8082/room/all?sourceFunding=NONE ``` get the same error: Parameter value [NONE] did not match expected type [Leo.booking.SourceFunding (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [NONE] did not match expected type [Leo.booking.SourceFunding (n/a)] – Alex Jul 15 '22 at 17:21
  • no no, you are not getting my point, in `tests` section, just use `console.log(pm.request.url.query)` & click on View->Show Postman Console, & hit your request normally, you should see what are parameters you are passing in console.log – Ashish Patil Jul 15 '22 at 17:25
  • I just want to check if you are passing any special character as a part of your input. if that is case, your code will fail – Ashish Patil Jul 15 '22 at 17:28
  • in tests section - you mean Postman section or intellij idea tests? – Alex Jul 15 '22 at 17:29
  • ```curl http://localhost:8082/room/all?sourceFunding=NONE``` get the same error – Alex Jul 15 '22 at 17:30
  • @Alex, what headers it is accepting at moment? – Ashish Patil Jul 15 '22 at 17:40
  • I think the input is correct, for example ```http://localhost:8082/room/all?sortDirection=ASC``` works well, this is the same postmann and the same method of the same controller – Alex Jul 15 '22 at 17:41
  • I am trying to create absolutely new project to replicate the error – Alex Jul 15 '22 at 17:44
  • maybe this is not my day - https://start.spring.io/ - this cannot start a new project because cannot see maven ....... attempt #12 .... cannot create any java class in new project.... – Alex Jul 15 '22 at 17:50
  • this is definitely not my day - no light at home :) – Alex Jul 15 '22 at 17:59
  • you can try tomorrow, no worries .. :-), let me know in case anything further required from my end. – Ashish Patil Jul 15 '22 at 19:07