gpt4 book ai didi

java - Spring Boot 无法从非字符串类型的字段的 POST 请求将 JSON 转换为 pojo

转载 作者:行者123 更新时间:2023-11-29 04:16:47 26 4
gpt4 key购买 nike

 public class Dog {
private String name;
private int weight;
//...getters and
//setters and constructor
}

Controller :

 @RequestMapping(value = "/dogs", method = RequestMethod.POST, 
produces = "application/json")
public void createDog(Dog dog) {
dr.save(dog);
}

当我用 json {"name":"bark", "weight":50} 调用端点时怎么办?我得到一个错误:

Failed to convert value of type 'null' to required type 'int'; nestedexception isorg.springframework.core.convert.ConversionFailedException: Failed toconvert from type [null] to type [int] for value 'null'; nestedexception is java.lang.IllegalArgumentException: A null value cannotbe assigned to a primitive type","objectName": "dog","field": "weight","rejectedValue": null,"bindingFailure": true,"code": "typeMismatch"

"message": "Validation failed for object='dog'. Error count: 1

编辑:我对 boolean 值和 double 值有同样的问题。我想我必须使用对象而不是基元?

最佳答案

将注解@RequestBody添加到create方法中的参数dog

关于java - Spring Boot 无法从非字符串类型的字段的 POST 请求将 JSON 转换为 pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51880988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com