杰克逊·米金(Jackson Mixin)在Pojo中无法使用json

时间:2019-06-10 16:36:36

标签: java json xml jackson jaxb

我的目标是xml到Pojo,Pojo到json。我已经使用jaxb将xml转换为pojo。现在我正在尝试使用杰克逊Jaxb将pojo转换为json。我在哪里得到以下json,它在json中生成JXBElement类文件,如下所示。

{
  "name" : "{http://xxx.xx.xx.xx.xx.xx.xx}CompositeResponse",
  "declaredType" : "xxx.xx.xx.xx.xx.xx.xxCompositeResponseType",
  "scope" : "javax.xml.bind.JAXBElement$GlobalScope",
  "value" : {
    "CompositeIndividualResponse" : [ {
      "ResponseMetadata" : {
        "ResponseCode" : "HS000000",
        "ResponseDescriptionText" : "Success"
      }
    } ]
  },
  "nil" : false,
  "globalScope" : true,
  "typeSubstituted" : false
}

如何删除名称,declaredType,scope,nil,globalScope,typeSubstituted ang以获得以下json

{
 "CompositeResponse":
 {
    "CompositeIndividualResponse" : [ {
      "ResponseMetadata" : {
        "ResponseCode" : "HS000000",
        "ResponseDescriptionText" : "Success"
      }
    } ]
  }
}

我一直在看这个post,但这对我不起作用。
我为杰克逊混音尝试的以下代码。

public class Main {
    public static interface JAXBElementMixinT {
        @JsonValue
        Object getValue();
    }
    public static void main(String[] args) throws XMLStreamException, IOException {

              ObjectMapper mapper = new ObjectMapper(); 
              AnnotationIntrospector introspector = new  JaxbAnnotationIntrospector(mapper.getTypeFactory());
              mapper.setAnnotationIntrospector(introspector );
              mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
              mapper.addMixIn(JAXBElement.class, JAXBElementMixinT.class); 
              String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
              System.out.println(result);

    }
}

我也尝试了以下代码,但是没有运气。

public abstract class JAXBElementMixIn {

    @JsonIgnore abstract String getScope();
    @JsonIgnore abstract boolean isNil();
    @JsonIgnore abstract boolean isGlobalScope();
    @JsonIgnore abstract boolean isTypeSubstituted();
    @JsonIgnore abstract Class getDeclaredType();
}

任何人都可以帮助我哪里做错了以及该怎么做。

2 个答案:

答案 0 :(得分:1)

事实上,我本周刚刚遇到了这个问题,并通过删除

解决了该问题
AnnotationIntrospector introspector = new  JaxbAnnotationIntrospector(mapper.getTypeFactory());
              mapper.setAnnotationIntrospector(introspector );

片。我还没有回过头来研究如何添加它,但是那让您剩下的代码可以为我正确地工作,而我再也看不到JAXBElement包装器了。

答案 1 :(得分:0)

最后,我能够解决问题。根据@Ryan的回答,我不需要以下代码: AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector );

但是我必须添加JaxbAnnotationModule module = new JaxbAnnotationModule(); mapper.registerModule(module),否则杰克逊将为每个元素生成链接和元数据。完整的代码如下

public class Main {
public static interface JAXBElementMixinT {
    @JsonValue
    Object getValue();
}
public static void main(String[] args) throws XMLStreamException, IOException {

          ObjectMapper mapper = new ObjectMapper(); 
          JaxbAnnotationModule module = new JaxbAnnotationModule(); 
          mapper.registerModule(module)
          mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
          mapper.addMixIn(JAXBElement.class, JAXBElementMixinT.class); 
          String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
          System.out.println(result);

}

}