简单的Spring Boot POST不起作用

斯曼斯菲尔德

我编写了一个非常简单的Spring Boot GET和POST示例。GET工作正常。出于某种原因,POST无法获取json对象。它只有零或零。这是其余的控制器代码,

@RestController
public class HttpWebServiceController {

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ResponseBody
    public String status() throws Exception {

        // Build Service Count String
        ++ServiceCount;
        ServerResponse = "\nSecure HTTPS WebService -- ServiceCount = ";
        ServerResponse += ServiceCount +"\n\n";

        return ServerResponse;

    }

    @RequestMapping(value = "/batteryupdate", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<BatteryData> update(@RequestBody BatteryData
        batterydata) {

        System.out.printf("Manufacturer: %s\n",
           batterydata.getManufacturerId());
        return new ResponseEntity<BatteryData>(batterydata, HttpStatus.OK);
    }

    String ServerResponse;
    int ServiceCount = 0;

}

GET / status工作正常。POST / updatebattery返回json字符串,但json字符串的所有元素均为null,浮点数为零。这是我的邮递员代码。

POST /batteryupdate HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 31c74432-9d32-e7a9-0446-242a24677d2b

{
    "ManufacturerId":"Hyster",
    "Voltage": 48.76,
    "Current": 18.27
}

这是发送的邮递员数据,

{
    "ManufacturerId":"Hyster",
    "Voltage": 48.76,
    "Current": 18.27
}

这是邮递员返回的结果。printf还返回null。

{
   "current": 0,
   "manufacturerId": null,
   "voltage": 0
}

知道为什么这个简单的spring boot POST程序不起作用吗?

地面

尝试发送名称为小写的json。如果不能解决问题,请发送不带json的json manufacturerId(如果枚举)

{
   "current": 0,
   "manufacturerId": "Hyster",
   "voltage": 0
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章