[英]Jackson fails to serialize Joda DateTimeFormatter
I am trying to return a JSON in my Spring MVC 3 application, but its failing for Joda DateTimeFormatter
我试图在我的Spring MVC 3应用程序中返回一个JSON,但它失败了Joda
DateTimeFormatter
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.joda.time.format.DateTimeFormat$StyleFormatter and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.HashMap["personDay"]->mypackage.PersonDay["dateTimeFormatter"]->org.joda.time.format.DateTimeFormatter["parser"])
It looks like i might need a custom serializer for this, but i am not sure where to begin. 看起来我可能需要一个自定义序列化器,但我不知道从哪里开始。
You can take a look here for more details and options. 您可以在这里查看更多详细信息和选项。
Basically, you need to create a Serializer
, something like: 基本上,您需要创建一个
Serializer
,如:
public class ItemSerializer extends StdSerializer<Item> {
public ItemSerializer() {
this(null);
}
public ItemSerializer(Class<Item> t) {
super(t);
}
@Override
public void serialize(Item value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("id", value.id);
jgen.writeStringField("itemName", value.itemName);
jgen.writeNumberField("owner", value.owner.id);
jgen.writeEndObject();
}
}
Then you can annotate your class with: @JsonSerialize
, something like: 然后你可以用:
@JsonSerialize
注释你的类,类似于:
@JsonSerialize(using = ItemSerializer.class)
public class Item {
public int id;
public String itemName;
public User owner;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.