什麼是DeserializationFeature.ACCEPT_FLOAT_AS_INT在交替傑克遜數據綁定2.5,因爲這反序列化配置不2.5可用(其可用自2.6)傑克遜-2.5數據綁定轉換浮動到int
我們的應用程序使用jackson-databind 2.5,其中我們注意到float正在轉換爲int。
雖然我們需要禁用價值轉換,強制。但是,正如我們所使用傑克遜2.5,有NODeserializationFeature.ACCEPT_FLOAT_AS_INT
如何禁用2.5本浮動轉變爲int的轉換?如果他在「age」中提供float,而不是int,我們期望用戶應該得到數據驗證異常。
下面是示例POJO
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
這是測試代碼(使用傑克遜 - 數據綁定2.5):的上述測試代碼
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestConvert {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\" : \"foobar\", \"age\" : 2.99}";
//JSON from file to Object
User user = mapper.readValue(json, User.class);
//Object to JSON in String
String jsonInString = mapper.writeValueAsString(user);
System.out.println(jsonInString);
}
}
輸出是:
{「名「:」foobar「,」age「:2}
可能的重複[Java傑克遜 - 防止浮動到int轉換時反序列化](http://stackoverflow.com/questions/25076030/java-jackson-pre當反序列化時通氣浮動到整型轉換) –