Jackson Mixin 不在 Pojo 工作 json

Jackson Mixin not working in Pojo to json

我的目标是 xml 到 Pojo,Pojo 到 json。我已经使用 jaxb 对 pojo 做了 xml。现在我正在尝试使用 jackson Jaxb 将 pojo json。我在哪里得到以下 json,它在 json 中生成 JXBElement class 文件,如下所示。

{
  "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
}

如何删除 name、declaredType、scope、nil、globalScope、typeSubstituted 并获得以下内容 json

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

我正在看这个 post 但这对我不起作用。
以下代码我已经为 jackson mixin 尝试过。

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();
}

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

实际上我这周刚遇到这个问题并通过删除

解决了它
AnnotationIntrospector introspector = new  JaxbAnnotationIntrospector(mapper.getTypeFactory());
              mapper.setAnnotationIntrospector(introspector );

件。我还没有回过头来研究如何将其添加回来,但是让您拥有的其余代码正确地为我工作并且我不再看到 JAXBElement 包装器。

终于可以解决问题了。根据@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);

}

}