Spring Boot POST 请求对象几乎所有空字段都带有 Avro class
Spring Boot POST request object has almost all null fields with Avro class
我们有以下简单的网络服务器:
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 生成的 class,它是根据以下 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"
}
]
}
我们向网络服务器发送以下 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
除了 charging
字段外还有所有 null
值,如下所示:
{"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 class 也是有效的。我们的 Spring Boot 服务器是从 Spring Boot 自己的文档中获取的简单有效的代码。我希望 Spring Boot 投射到 BatteryEvent
对象的数据包含发送的电池事件 JSON.
的值
我不确定你的问题的根本原因是什么,但似乎有一些问题
JSON serialization/deserialization.
当我尝试重现您的问题时,根据您的信息,我遇到了一些异常,我使用此处提到的解决方案解决了该问题 here
在此之后,当我执行 POST API 时,我收到了包含所有值的预期结果。
你可以在这里查看我的代码here
您能否提供有关您的代码的更多信息,例如您是否修改了默认的 serialization/deserialization 流程?这将有助于更多地了解为什么在您的代码中将属性设置为 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
}
实体中的字段名应与json中的键名相同
我们有以下简单的网络服务器:
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 生成的 class,它是根据以下 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"
}
]
}
我们向网络服务器发送以下 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
除了 charging
字段外还有所有 null
值,如下所示:
{"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 class 也是有效的。我们的 Spring Boot 服务器是从 Spring Boot 自己的文档中获取的简单有效的代码。我希望 Spring Boot 投射到 BatteryEvent
对象的数据包含发送的电池事件 JSON.
我不确定你的问题的根本原因是什么,但似乎有一些问题 JSON serialization/deserialization.
当我尝试重现您的问题时,根据您的信息,我遇到了一些异常,我使用此处提到的解决方案解决了该问题 here
在此之后,当我执行 POST API 时,我收到了包含所有值的预期结果。
你可以在这里查看我的代码here
您能否提供有关您的代码的更多信息,例如您是否修改了默认的 serialization/deserialization 流程?这将有助于更多地了解为什么在您的代码中将属性设置为 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
}
实体中的字段名应与json中的键名相同