2016-03-09 90 views
0

我有一个简单的注解类:如何序列注释与杰克逊

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.TYPE) 
public @interface WebService { // with methods} 

而且一个POJO

class Pojo { 
    Webservice webservice; 
} 

每当我试图序列Pojo,各个领域得到除Webservice场序列化。

我对反序列化只有序列化不感兴趣。

这是杰克逊的限制吗?

+0

你的问题是不是序列化的注释。对序列化感兴趣但不是反序列化不计算。不清楚你要问什么,直到没有任何意义。 – EJP

+0

我的问题正是关于序列化注释。 –

回答

0

一个很好的问题。如果您在批注类型方法中添加@JsonProperty批注,则序列化可以正常工作。这里有一个例子:

@JacksonAnnotationSerialization.MyAnnotation(a = "abc", b = 123) 
public class JacksonAnnotationSerialization { 

    @Retention(RetentionPolicy.RUNTIME) 
    @interface MyAnnotation { 
     @JsonProperty 
     String a(); 
     @JsonProperty 
     int b(); 
    } 

    static class Thing { 
     public final String field; 
     public final MyAnnotation myAnnotation; 

     Thing(final String field, final MyAnnotation myAnnotation) { 
      this.field = field; 
      this.myAnnotation = myAnnotation; 
     } 
    } 

    public static void main(String[] args) throws JsonProcessingException { 
     final ObjectMapper objectMapper = new ObjectMapper(); 
     final MyAnnotation annotation 
       = JacksonAnnotationSerialization.class.getAnnotation(MyAnnotation.class); 
     final Thing thing = new Thing("value", annotation); 
     System.out.println(objectMapper.writeValueAsString(thing)); 
    } 
} 

输出:

{"field":"value","myAnnotation":{"a":"abc","b":123}}