我正在尝试使用可选的请求参数/请求部分,但是当我不提供可选参数时,我的请求就会无限期地挂起。
@RestController
@RequestMapping("/service")
class MyController {
@PostMapping
fun print(@RequestPart("name", required = false) name: String) {
if (name != null)
print(name)
else
print("grr")
}
}
如果我在请求中提供参数name
,它不再明确地卡在 postman 中,请求会通过。但当我不提供参数 name
并打印“grr”时,我希望它无论如何都会通过。
当您添加另一个所谓的可选属性时,会验证此必需属性无法正常工作(至少在我看来)。
@RestController
@RequestMapping("/service")
class MyController {
@PostMapping
fun print(@RequestPart("name", required = false) name: String,
@RequestPart("friend_name", required = false) friendsName: String) {
if (name != null)
print(name)
else
print("grr")
}
}
现在,当我提供参数 name
但不提供 friend_name
时,它表示该值不能为 null。
{
"timestamp": "2018-10-10T09:50:49.305+0000",
"path": "/service",
"status": 500,
"error": "Internal Server Error",
"message": "Parameter specified as non-null is null: method co.example.controllers.MyController.print, parameter friendsName"
}
我对 @RequestParam
和 @RequestPart
进行了相同的尝试,得到了相同的结果。
最佳答案
将可选参数标记为可为空。因此:
@RequestPart("name", required = false) name: String?
@RequestPart("friend_name", required = false) friendsName: String?
关于java - Spring Webflux : optional RequestPart/RequestParam is still required?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737540/