【问题标题】:Jackson Mixin not working in Pojo to jsonJackson Mixin 不能在 Pojo 中工作到 json
【发布时间】:2019-06-10 16:36:36
【问题描述】:

我的目标是 xml 到 Pojo 和 Pojo 到 json。我已经使用 jaxb 对 pojo 做了 xml。现在我正在尝试使用 jackson 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
}

我怎样才能删除name、declaredType、scope、nil、globalScope、typeSubstituted ang得到下面的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();
}

谁能帮助我哪里错了,该怎么办,谢谢。

【问题讨论】:

    标签: java json xml jackson jaxb


    【解决方案1】:

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

    AnnotationIntrospector introspector = new  JaxbAnnotationIntrospector(mapper.getTypeFactory());
                  mapper.setAnnotationIntrospector(introspector );
    

    一块。我还没有回过头来研究如何添加它,但这让你拥有的其余代码正确地为我工作,我不再看到 JAXBElement 包装器。

    【讨论】:

    • 您的解决方案几乎是正确的。而不是 AnnotationIntrospector,我们需要以下代码来将 jackson 注册为 jaxb 模块。 JaxbAnnotationModule 模块 = 新 JaxbAnnotationModule(); mapper.registerModule(模块);否则会产生一些垃圾数据。
    【解决方案2】:

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

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2022-07-24
      • 2020-10-18
      相关资源
      最近更新 更多