SpringBootPostRequest对象几乎所有的空字段都带有avro类

iih3973s  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(277)

我们有以下简单的Web服务器:

package spring;

import avro.BatteryEvent;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;

@RestController
public class EnergyResourcesController {
    private final Date date = new Date();

    @PostMapping("/event/{uuid}")
    BatteryEvent postBatteryEvent(
        @PathVariable("uuid") String uuid,
        @RequestBody BatteryEvent batteryEvent) throws IOException {
        batteryEvent.setTime(date.getTime());
        return batteryEvent;
    }
}

它使用一个 BatteryEvent ,这是一个avro生成的类,它是根据以下avro架构构建的:

{
  "namespace": "avro",
  "type": "record",
  "name": "BatteryEvent",
  "fields": [
    {
      "name": "charging_source",
      "type": [
        "string",
        "null"
      ]
    },
    {
      "name": "processor4_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "device_id",
      "type": [
        "string",
        "null"
      ]
    },
    {
      "name": "processor2_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "processor1_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "charging",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "current_capacity",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "inverter_state",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "moduleL_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "moduleR_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "processor3_temp",
      "type": [
        "int",
        "null"
      ]
    },
    {
      "name": "soC_regulator",
      "type": [
        "float",
        "null"
      ]
    },
    {
      "name": "time",
      "type": [
        "long",
        "null"
      ],
      "logicalType": "local-timestamp-millis"
    }
  ]
}

我们向Web服务器发送以下json,

{
  "charging_source": "utility",
  "processor4_temp": 160,
  "device_id": "18806072-81ca-48ed-b01a-b080f2d8a1fa",
  "processor2_temp": 96,
  "processor1_temp": 60,
  "charging": -912,
  "current_capacity": 10948,
  "inverter_state": 1,
  "moduleL_temp": 199,
  "moduleR_temp": 91,
  "processor3_temp": 152,
  "soC_regulator": 26.598085
}

问题在于 BatteryEvent 我们所得到的一切 null 价值观 charging 场,就像这样:

{"charging_source": null, "processor4_temp": null, "device_id": null, "processor2_temp": null, "processor1_temp": null, "charging": -261, "current_capacity": null, "inverter_state": null, "moduleL_temp": null, "moduleR_temp": null, "processor3_temp": null, "soC_regulator": null, "time": null}

我的问题是,为什么会这样?所有发送的数据都是有效的json,我们的avro类也是有效的。我们的springboot服务器是从springboot自己的文档中提取的简单有效的代码。我想要spring boot向 BatteryEvent 对象来包含发送的电池事件json中的值。

vmjh9lq9

vmjh9lq91#

我不确定您的问题的根本原因是什么,但是json序列化/反序列化似乎有一些问题。
当我试图复制你的问题,根据你的信息,我得到一些例外,我解决了使用这里提到的解决方案
之后,当我执行postapi时,我收到了预期的结果,并填充了所有的值。
你可以在这里查我的密码
你能提供更多关于你的代码的信息吗,比如你是否修改了默认的序列化/反序列化过程?这将有助于更好地了解为什么在代码中属性被设置为 null 下面是我的例子中的请求和响应
请求

{
    "charging_source": "utility",
    "processor4_temp": 160,
    "device_id": "18806072-81ca-48ed-b01a-b080f2d8a1fa",
    "processor2_temp": 96,
    "processor1_temp": 60,
    "charging": -912,
    "current_capacity": 10948,
    "inverter_state": 1,
    "moduleL_temp": 199,
    "moduleR_temp": 91,
    "processor3_temp": 152,
    "soC_regulator": 26.598085
}

响应

{
    "charging_source": "utility",
    "processor4_temp": 160,
    "device_id": "18806072-81ca-48ed-b01a-b080f2d8a1fa",
    "processor2_temp": 96,
    "processor1_temp": 60,
    "charging": -912,
    "current_capacity": 10948,
    "inverter_state": 1,
    "moduleL_temp": 199,
    "moduleR_temp": 91,
    "processor3_temp": 152,
    "soC_regulator": 26.598085,
    "time": 1609404352570,
    "moduleRTemp": 91,
    "moduleLTemp": 199,
    "inverterState": 1,
    "soCRegulator": 26.598085,
    "deviceId": "18806072-81ca-48ed-b01a-b080f2d8a1fa",
    "processor1Temp": 60,
    "currentCapacity": 10948,
    "processor3Temp": 152,
    "processor4Temp": 160,
    "chargingSource": "utility",
    "processor2Temp": 96
}
p4rjhz4m

p4rjhz4m2#

实体中的字段名应与json中的键名相同

相关问题