且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring 3.2和Jackson 2:添加自定义对象映射器

更新时间:2023-01-17 12:27:03

您应该使用 @JsonSerialize annontation。在你的情况下,它可能是:

You should annotate corresponding model field with @JsonSerialize annontation. In your case it may be:

public class MyMongoModel{
   @JsonSerialize(using=ObjectIdSerializer.class)
   private ObjectId id;
}

但在我看来,***不要将实体模型用作VO 。更好的方法是在它们之间建立不同的模型和映射。
您可以在这里找到我的示例项目(我使用Spring 3和Jackson 2作为示例的日期序列化。)

But in my opinion, it should better don't use entity models as VOs. Better way is to have different models and map between them. You can find my example project here (I used date serialization with Spring 3 and Jackson 2 as example).