Marek Raki
Marek Raki

Reputation: 3136

Serializing object in Jackson and @XmlValue

Bean:

@XmlRootElement(name = "integer")
@XmlAccessorType(XmlAccessType.FIELD)
public class IntegerWrapper implements Serializable {

  private static final long serialVersionUID = -455136597629446657L;

  @XmlValue
  private Integer value;

   // getters/setter (no annotations)
  }

Code:

ObjectMapper mapper = new ObjectMapper();

IntegerWrapper iw = new IntegerWrapper();
iw.setValue(new Integer(11));
System.out.println("IntegerWrapper\n" + mapper.writeValueAsString(iw));

Result:

{"value":11,"fieldHandler":null}

Why is "fieldHandler" in the resulting object, and how do I get rid of it?

UPDATE:

Appears only when I run it through maven or as a web service where Jackson is the JSON provider. IntegerWrapper is in the package where are some JPA beans but IntegerWrapper is not mentioned in persistence.xml. When I copy it to another package fieldHandler disappears.

Upvotes: 1

Views: 464

Answers (1)

Paul Whelan
Paul Whelan

Reputation: 16809

I searched for fieldHandler and it looks to be related to hibernate, I suspect that your object is managed by hibernate.

I had a quick look and I found jackson-datatype-hibernate perhaps this can be used to register a jackson module to get the results you desire.

Upvotes: 1

Related Questions