JAXB return null instead empty string

后端 未结 2 1692
醉梦人生
醉梦人生 2021-02-03 13:35

How I can retrieve null value, when unmarshalling, if inside XML attribute value is empty ? Now I make inside my getters checking for null :

         


        
2条回答
  •  终归单人心
    2021-02-03 14:02

    I think your XML looks more or less like this:

        
    

    This, unfortunately, means, that you are passing an empty string.

    If you want to pass null you have two options:

    1. Do not pass this tag at all (your XML should not contain tag at all).
    2. Use xsi:nil.

    If using xsi:nil, first you have to declare your xml element (in XSD file) as nilable, like this:

        
    

    Then, to pass the null value inside XML do this:

        
    

    or this:

        
    

    This way, JAXB knows, that you are passing null instead of an empty String.

提交回复
热议问题