2017-09-09 137 views
0

我有註釋是這樣的:ObjectMapper避免註釋

@Valid 
@NotNull 
@JsonTypeResolver(com.rsqtechnologies.sioms.shared.JsonTypeResolver.class) 
@JsonTypeInfo(
     use = JsonTypeInfo.Id.NAME, 
     include = JsonTypeInfo.As.PROPERTY, 
     property = "type", 
     visible = true) 
@JsonSubTypes({ 
     @JsonSubTypes.Type(value = HandballPlayerTO.class, name = "handballPlayerTO"), 
     @JsonSubTypes.Type(value = FootballPlayerTO.class, name = "footballPlayerTO") 
}) 

而且想使用objectMapper期間避免@JsonTypeResolver。

我已經試過這樣:

ObjectMapper mapper = new ObjectMapper(); 
    JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() { 
     @Override 
     protected TypeResolverBuilder<?> _findTypeResolver(
       MapperConfig<?> config, Annotated ann, JavaType baseType) { 
      if (!ann.hasAnnotation(JsonTypeResolver.class)) { 
       return super._findTypeResolver(config, ann, baseType); 
      } 
      return null; 
     } 
    }; 
    mapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector); 

但正如我注意到它刪除了旁邊顯示的所有註釋。

回答

0

_findTypeResolver方法處理@JsonTypeResolver,@JsonTypeInfo和@JsonTypeIdResolver。通過重寫此方法,可以防止Jackson處理所有這些問題。而不是重寫_findTypeResolver,重寫_findAnnotation,以便傑克遜仍然可以看到其他人。

JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() { 
    protected <A extends Annotation> A _findAnnotation(Annotated annotated, Class<A> annoClass) { 
     return JsonTypeResolver.class == annoClass ? null : annotated.getAnnotation(annoClass); 
    } 
}; 
+0

真棒多數民衆贊成正是我一直在尋找。 – MrNetroful