首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Jackson Mixin不在Pojo to json中工作

Jackson Mixin不在Pojo to json中工作
EN

Stack Overflow用户
提问于 2019-06-11 00:36:37
回答 2查看 272关注 0票数 0

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

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
{
 "CompositeResponse":
 {
    "CompositeIndividualResponse" : [ {
      "ResponseMetadata" : {
        "ResponseCode" : "HS000000",
        "ResponseDescriptionText" : "Success"
      }
    } ]
  }
}

我正在寻找这个post,但这对我不起作用。

下面是我为jackson mixin尝试过的代码。

代码语言:javascript
复制
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);

    }
}

我也尝试了下面的代码,但没有成功。

代码语言:javascript
复制
public abstract class JAXBElementMixIn {

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

有人能帮我吗?我哪里错了?该怎么做?谢谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56530267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档