0

My target is xml to Pojo and Pojo to json. I already did xml to pojo using jaxb. Now I am trying pojo to json using jackson Jaxb. Where I am getting the following json which produce JXBElement class fileds in json like following.

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

How Can I removed name, declaredType,scope,nil, globalScope,typeSubstituted ang get the following json

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

I was looking this post but this does not work for me.
The following code I have tried for 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);

    }
}

I also tried with following code but no luck.

public abstract class JAXBElementMixIn {

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

Can anyone help me where I am wrong and what to do thanks.

Mohammad Hasan
  • 123
  • 1
  • 6
  • 22

2 Answers2

1

I just faced this issue this week in fact and solved it by removing the

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

Piece. I haven't looked back into how to add that back yet, but that let the rest of the code you have work for me correctly and I'm no longer seeing the JAXBElement wrapper.

Ryan
  • 11
  • 1
  • Your solution is almost correct. Rather than AnnotationIntrospector we need the following code to register jackson as jaxb module. JaxbAnnotationModule module = new JaxbAnnotationModule(); mapper.registerModule(module); Otherwise some garbage data will produce. – Mohammad Hasan Jun 10 '19 at 21:33
1

Finally I am able to solve the problem. According @Ryan answer I donot need the following code: AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper.getTypeFactory()); mapper.setAnnotationIntrospector(introspector );

But I have to add the JaxbAnnotationModule module = new JaxbAnnotationModule(); mapper.registerModule(module) otherwise jackson will produce links and metadata for every element. The complete code is following

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

}

}

Mohammad Hasan
  • 123
  • 1
  • 6
  • 22