Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

java - Spring Boot POST request object has almost all null fields with Avro class

We have the following simple webserver:

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;
    }
}

It uses a BatteryEvent, which is an Avro-generated class, which is built from the following Avro schema:

{
  "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"
    }
  ]
}

We send the webserver the following 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
}

The problem is the BatteryEvent that we receive has all null values besides the charging field, like so:

{"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}

My question is, why is this? All of the sent data is valid JSON, and our Avro class is valid as well. Our Spring Boot server is simple and valid code that was taken from Spring Boot's own documentation. I want the data which Spring Boot casts to the BatteryEvent object to contain the values from the sent battery event JSON.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am not sure what is the root cause of your problem, but it seems there is some issue with JSON serialization/deserialization.

When I tried to replicate your issue, as per your information I was getting some exception which I resolved by using the solution mentioned here here

After this when I execute the POST API I received the expected result with all the values populated.

You can check my code here here

Can you please provide more information about your code, like if you have modified the default serialization/deserialization process ? This will help to get more idea about why in your code the properties are getting set as null

Below are the request and response from my example

Request

{
    "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
}

Response

{
    "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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...