简体   繁体   English

Spring Webflux:仍然需要可选的RequestPart / RequestParam吗?

[英]Spring Webflux: optional RequestPart/RequestParam is still required?

I'm trying to use optional request parameters / request parts, but when I don't provide an optional parameter, my request just hangs indenfinitely. 我正在尝试使用可选的请求参数/请求部分,但是当我不提供可选参数时,我的请求将无限期地挂起。

@RestController
@RequestMapping("/service")
class MyController {
    @PostMapping
    fun print(@RequestPart("name", required = false) name: String) {
        if (name != null)
            print(name)
        else
            print("grr")
    }
}

If I provide the parameter name in my request, it no longer hangs definitely in postman, the request goes through. 如果我在请求中提供参数name ,则该name将不再挂在邮递员中,请求通过。 But I expected it to go through anyway when I don't provide the parameter name and print "grr". 但是我希望它在我不提供参数name并显示“ grr”时仍然会通过。

This required property not working properly (at least in my mind) is validated when you add another supposedly optional property. 当您添加另一个假定的可选属性时,此必需的属性无法正常工作(至少在我看来)。

@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")
    }
}

Now when I provide the parameter name but not friend_name , it says the value cannot be null. 现在,当我提供参数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"
}

I have tried the same for @RequestParam and @RequestPart with the same results. 我已经为@RequestParam@RequestPart尝试了相同的结果。

Mark your optional parameters as nullable. 将您的可选参数标记为可为空。 Therefore: 因此:

@RequestPart("name", required = false) name: String?
@RequestPart("friend_name", required = false) friendsName: String?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM