[英]@RequestParam and mapping enum
I have a problem with enum as @RequestParam
.我对枚举作为@RequestParam
有疑问。 In the method getRooms
there are two enums: Sort.Direction
and SourceFunding
.在getRooms
方法中有两个枚举: Sort.Direction
和SourceFunding
。 The Sort.Direction
is the build-in Spring enum, and it convert from String to Enum perfectly: Sort.Direction
是内置的 Spring 枚举,它可以完美地从 String 转换为 Enum:
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 的 @RequestParam 与 Enum
Spring boot able to accept Enum as Request parameter Spring Boot 能够接受 Enum 作为请求参数
Enums as Request Parameters in Spring Boot Rest 枚举作为 Spring Boot Rest 中的请求参数
How to convert multiple values to enum in RequestParm? 如何在RequestParm中将多个值转换为枚举?
@RequestParam defaultvalue does not accept enum value as a default value @RequestParam defaultvalue 不接受枚举值作为默认值
Accept and parse integer for Enum type query parameter in Java 接受并解析 Java 中枚举类型查询参数的整数
@RequestParam with a list of parameters @RequestParam 带有参数列表
and many others with no effect.和许多其他没有效果。 Also I have tried to emulate Sort.Direction in my enum.我也尝试在我的枚举中模拟 Sort.Direction。
****** 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" }错误:{“时间戳”:“2022-07-15T15:53:27.371+00:00”,“状态”:400,“错误”:“错误请求”,“消息”:“无法转换类型的值'java.lang.String' 到所需类型 'xxx.xxxxx.Leo.booking.SourceFunding';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [ @org.springframework.web.bind.annotation.RequestParam xxx.xxxxx.Leo.booking.SourceFunding] 用于值“PUBLIC_INSURANCE”;嵌套异常是 java.lang.IllegalArgumentException:没有枚举常量 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)];参数值 [PRIVATE_INSURANCE] 与预期类型 [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)]嵌套异常是 java.lang.IllegalArgumentException:参数值 [PRIVATE_INSURANCE] 与预期类型不匹配 [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...更新1 - 这太疯狂了......
SourceFunding.valueOf(sourceFundingString); => Error 'No enum constant ...'
log.info("!! {}", Arrays.toString(SourceFunding.values())); => Return all enum's fields
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'
.错误显示它在您的输入'PUBLIC_INSURANCE'
中有单引号。
Spring uses Enum.valueOf(string)
to evaluate queryParam value. Spring 使用Enum.valueOf(string)
来评估 queryParam 值。 If we use 'PUBLIC_INSURANCE'
,it will not find any corresponding enum.如果我们使用'PUBLIC_INSURANCE'
,它将找不到任何对应的枚举。
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(pm.request.url.query)
在邮递员的tests
部分记录您的输入请求并检查您的输入是否正确。
Console log in postman can be visible by clicking View
-> Show Postman Console
通过单击View
-> Show Postman Console
可以看到 postman 中的控制台日志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.