Feign 发出空请求

问题描述 投票:0回答:1

我使用了 eureka 和 openfeign,我想发送简单的帖子,但它会清空请求正文中的字段。

以下是客户端的定义

@FeignClient("notification", path = "/api")
interface NotificationClient {

    @RequestMapping(
        method = [RequestMethod.POST],
        path = ["/sms"],
        consumes = [MediaType.APPLICATION_JSON_VALUE],
        name = "sendSms"
    )
    fun sendSms(@RequestBody(required = true) message: Message)
}

身体:

@MarkAsOpen
class Message(
    val phone: String,
    val text: String,
    val uuid: UUID = UUID.randomUUID()
) {

在其他服务中我收到字段为空的错误

2024-03-19 08:33:00.070 WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public void tralalala.notification.sms.control.SmsResource.sendSms(tralalala.sms.Message): [Error in object 'tralalala.sms.Message': codes []; arguments []; default message [Parameter specified as non-null is null: method tralalala.sms.Message.<init>, parameter phone]] ]

当我通过外部工具调用此端点时,它就可以工作。看起来 Feign 客户端使 body 无效。

spring spring-boot netflix-eureka spring-cloud-feign openfeign
1个回答
0
投票

我已经解决了这个问题。由于与肥皂端点冲突,我还必须在 Rest 端点上标记 @RequestBody,现在反序列化工作正常

    @PostMapping("$API_URI/sms")
    fun sendSms(@RequestBody message: Message) {
        //logic
    }
© www.soinside.com 2019 - 2024. All rights reserved.