【发布时间】:2020-07-24 09:11:44
【问题描述】:
我在控制器中有以下内容,可以精美地捕获来自 QueryString 的 ProducerConfig 设置
[HttpPost("mylink/load/")]
public async Task<IActionResult> PostMessageAsync(CancellationToken cancellationToken, [FromQuery] ProducerConfig producerConfigs)
但是,如果我决定更改为从请求正文而不是 QueryString 中捕获参数,我将无法做到。
public async Task<IActionResult> PostMessageAsync(CancellationToken cancellationToken, [FromBody] ProducerConfig producerConfigs)
我使用以下 JSON 正文向 Postman 发出请求:
{
"LingerMs" : 420,
"BatchNumMessages" : 5,
"CompressionLevel" : 2,
"CompressionType" : 1
}
并因 400 错误请求而失败。
回应
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|246f628d-4624827bfa8633b1.","errors":{"$":["The JSON value could not be converted to Confluent.Kafka.ProducerConfig. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}
【问题讨论】:
-
嗨,@Saher,你在使用 [FromBody] 时设置了 PostMessageAsync HttpPost 吗?
-
这是一个帖子,所以我不确定它为什么会这样。我最终使用了另一个类并将属性转换为
ProducerConfig -
我看不到你的 ProducerConfig 模型。从响应中,您应该检查您的属性类型或其在 Confluent.Kafka.ProducerConfig 中的验证。
-
安装Newtonsoft.JSON包自动转换。
标签: asp.net-mvc asp.net-core asp.net-web-api apache-kafka confluent-platform