📜  jackson objectmapper 接受 float 作为 int - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:02.190000             🧑  作者: Mango

代码示例1
class Student {

    private int age;    
    public int getAge() {
        return age;
    }

    public void setAge(String ageString) {
        System.out.println("called");
        try {
            age = Integer.parseInt(ageString);
        } catch (NumberFormatException e) {
           throw new IllegalArgumentException("age can't be in float");
        }
    }
}

...

try {
    Student student = new ObjectMapper().readValue("{\"age\": 12.5}", Student.class);
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}