我是 jackson 的新手。我试图将 POJO(普通的旧 java 对象)序列化为 JSON,但我不断收到 JsonProcessingException。我实在不明白为什么。我错过了什么吗?
public class Car {
private String make;
private String model;
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Car (String make, String model) {
this.make = make;
this.model = model;
}
}
然后在其他地方我有......
try {
Car car = new Car("honda", "accord");
String serialized;
// enable pretty formatting
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
serialized = mapper.writeValueAsString(car);
System.out.println(serialized);
} catch (JsonProcessingException e) {
System.out.println("Failed to serialize the object to JSON");
}
我的输出很简单
Failed to serialize the object to JSON
这里可能出了什么问题?我不明白如何抛出这个异常。
最佳答案
检查 catch block 中抛出的异常中的消息以了解更多详细信息:
e.getMessage()
或者只是刷新堆栈跟踪
e.printStacktrace()
关于java - 尝试 writeValueAsString 时抛出 JsonProcessingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36025052/