首页 文章

Spring Boot无法将jSON转换为pojo,因为post请求不是string类型的字段

提问于
浏览
1

公共类Dog {私有字符串名称;私人重量; ... getter和setter以及构造函数}

调节器

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

为什么我用json {“name”:“bark”,“weight”:50}调用 endpoints 时出现错误:

“无法将类型'null'的值转换为必需的类型'int';嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型[null]转换为类型[int],值为'null';嵌套异常是java.lang.IllegalArgumentException:无法将null值赋给基本类型“,”objectName“:”dog“,”field“:”weight“,”rejectedValue“:null,”bindingFailure“:true,”code “: “类型不匹配”

“message”:“对象='狗'验证失败 . 错误计数:1”,

编辑:我得到了与布尔和双打相同的问题 . 我想我必须使用不是原始的对象?

3 回答

  • 0

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

  • 2

    仅适用于对象,因此请使用Integer而不是int .

  • 0

    如果存在可能性对象接收null,则不能使用类型原语,相同的int,char,boolean .

    private int weight
    

    使用类型对象相同的Integer,Boolean ...对于一个类型的原语有一个Object,然后可以接收null值 .

相关问题