我有一个问题,enum作为@RequestParam
。方法getRooms
中有两个枚举:Sort.Direction
和SourceFunding
。Sort.Direction
是内置的Spring枚举,它可以完美地从String转换为enum:
http://address:port/all?sortDirection=ASC => OK
http://address:port/all?sortDirection=DESC => OK
但是当我调用
http://address:port/all?sourceFunding=PUBLIC_INSURANCE => ALWAYS ERROR.
我已经阅读和测试了:
春# 39;s @RequestParam Enum
Spring boot能够接受Enum作为请求参数
枚举作为Spring Boot Rest的请求参数
如何将多个值转换为RequestParm中的enum ?
@RequestParam defaultvalue不接受enum值作为默认值
接受并解析Java中Enum类型查询参数的整数
带参数列表的@RequestParam
和许多其他的都没有效果。我也试着模仿Sort。Direction in my enum
******代码*******
错误:{"timestamp"2022 - 07 - 15 - t15:53:27.371 + 00:00","status" 400,错误";请求错误";message": "转换java.lang类型的值失败。字符串'到所需类型'xxx.xxx . leo .booking. sourcefunding ';org.springframework.core.convert.ConversionFailedException:从类型转换失败。输入[@org.springframework.web.bind.annotation]。RequestParam xxx.xxxxx.Leo.booking。SourceFunding]为值"PUBLIC_INSURANCE";嵌套异常是java.lang.IllegalArgumentException: No enum constant xxx.xxx . leo .booking. sourcefunding .'PUBLIC_INSURANCE'"path"/室/all"}
也可以是
参数值[PRIVATE_INSURANCE]与期望类型[xxx.xxx . leo .booking]不匹配。SourceFunding (n/a)];嵌套异常是java.lang.IllegalArgumentException:参数值[PRIVATE_INSURANCE]与预期类型[xxx.xxx . leo .booking]不匹配。SourceFunding (n/a)]
控制器:
@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
) {...}
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
这看起来对我来说,而使用邮差,你的查询参数输入没有得到正确的值。错误显示它有一个单引号在你的输入'PUBLIC_INSURANCE'
.
Spring使用Enum.valueOf(string)
计算queryParam值。如果使用'PUBLIC_INSURANCE'
,它将找不到任何相应的enum。
你可以试着在邮差的tests
部分通过console.log(pm.request.url.query)
&检查你的输入是否正确。
控制台登录邮差可以通过点击View
->Show Postman Console