【发布时间】:2021-04-01 22:32:07
【问题描述】:
这是我们的控制器
@GetMapping(path = "/getUsersForHierarchy/{entity}/{hierId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<UserInfo>> getUsersForHierarchy(@PathVariable(name = "entity", required = true) int entity,
@PathVariable(name = "hierId", required = true) Integer hierId) {
..................
..................
}
我们期待两个Path variables,一个是int 类型(实体),另一个是Integer 类型(hierId)。 hierId 可以为 null 或任何数值,这就是为什么它保留为 Wrapper。但是它在触发这样的请求时会出错
http://localhost:5010/v1/accountholder/userinfo/getUsersForHierarchy/5/null
现在 Wrapper 应该接受 null 值,但我们遇到了这样的错误
无法将“java.lang.String”类型的值转换为所需的“java.lang.Integer”类型;嵌套异常是 java.lang.NumberFormatException: For input string: "null"
如果我们将 Integer 更改为 String,则调用将进入控制器,但为了进一步处理,我们需要使用 Integer。
我们查看了这个Spring MVC @Path variable with null value,并尝试使用这样的 URL 更改来调用 API
http://localhost:5010/v1/accountholder/userinfo/getUsersForHierarchy/5/blaaa/null,但是报错还是一样。
应该怎么做?
【问题讨论】:
标签: spring-boot null integer wrapper path-variables