8

I have a DTO class with a field such as:

@XmlAttribute
@NotNull
private Integer number = null;

I'm trying to unmarshal xml such as

...  number=""  ...

I need the nuber field to stay null, so that a validation exception would be thrown. Instead JAXB unmarshals it as 0. How can I make it to behave correctly ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
RA.
  • 1,405
  • 1
  • 11
  • 18

1 Answers1

9

Arguable, it is behaving correctly. number="" does not mean null, it's an empty String, and JAXB is having to try and handle that correctly, and it decides that the closest thing to empty string for an Integer data type is zero. If you wanted a null, then the number attribute should be omitted altogether.

If you want to customise this behaviour, you need to write a subclass of javax.xml.bind.annotation.adapters.XmlAdapter which can handle the conversion between raw String and the boundtype (i.e. between String and Integer) in the way you want. You then wire up that adaptor by annotating the field with @XmlJavaTypeAdapter.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    I disagree completely with this being a correct behavior. There is no implicit conversion between empty string and an int ( for example Integer.parseInt("") throws NumberFormatException in this case. ). And it's not that I want a null - I'm unmarshalling client input, and the error codes that are returned should be the same in the case that the number attribute was omitted, or had no value. – RA. Feb 27 '11 at 12:55
  • 2
    @RA: You may disagree, fair enough, but equally I strongly disagree that an empty string is "no value". It *is* a value, it's an empty String, and not the same as a `null`. But regardless of what either of us think is "correct", all that matters is what JAXB thinks. – skaffman Feb 27 '11 at 13:02
  • empty string is not a "no-value", it's just a "not convertible to int value" :) – RA. Feb 27 '11 at 13:35
  • 2
    You can use schema validation to force numeric types to have a value: http://bdoughan.blogspot.com/2010/12/jaxb-and-marshalunmarshal-schema.html – bdoughan Feb 27 '11 at 13:42
  • @skaffman: I've tried your solution, but unfortunately it doesn't works for me, because if I throw a NumberFormatException from my XmlAdapter, JAXB just swallows it, and proceeds with the unmarshalling (unlike if it's own adapter - in this case the exception is not swallowed). So I can't create a cohesive API - it will display different behavior in cases where the attribute value is "abc" and when it's "", which is unacceptable. – RA. Feb 27 '11 at 14:50
  • 1
    @RA: Why are you throwing an exception? If the input String is blank, just return a `null` - that's what you wanted, isn't it? – skaffman Feb 27 '11 at 15:02
  • @RA, It is working for Long, but it is not working for Integer. But both are same wrapper class. I cannot use adapter class because inside adapter class it is coming as 0 instead of 0. Please let me know once you solve this problem. – deadend Mar 13 '17 at 10:21