简体   繁体   中英

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.

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
}

The field name in entity should be the same as the key name in json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM