0

I have a generated Java Class which is annotated with @XMLElement on the fields to change the name from upper to lower case:

class RECTYPE {
    @XmlElement(name = "simple")
    public String SIMPLE;    

    @XmlElement(name = "bool")
    public Boolean BOOL;
}

I know I could use @JsonProperty (Change field name in JSON using Jackson) to get lower case Json but I don't want to change my generated class.

Can I somehow use the name property of the @XMLElement Annotation that I have and tell Jackson to use it?

Thanks!

2 Answers2

0

Thanks to jaudo - the question was already answered here: Change field name in JSON using Jackson

I used the following snipped to use the @XMLElement annotations:

objectMapper.setAnnotationIntrospector(AnnotationIntrospector.pair( new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()), new JacksonAnnotationIntrospector()));

0

You can also use Mix-in annotations on an other class. This migth be useful in your case, the Mix-in class would look like this :

abstract class RectypeMixIn {

  @JsonProperty("simple") abstract String getSimple(); // rename property
  @JsonProperty("bool") abstract Boolean getBool(); // rename property  
}

And cofigure your ObjectMapper this way :

objectMapper.addMixInAnnotations(RECTYPE.class, RectypeMixIn.class);

Check this the doc here for more details

Youssef NAIT
  • 1,362
  • 11
  • 27
  • Thanks! That would also be a way but I would have to generate way more Classes. With the other solution I can use basically one line of code to use the already existing annotation! But it might be useful for someone else! – Nico Smeenk Jan 25 '19 at 07:10