Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:06:33+00:00 2026-06-03T09:06:33+00:00

How do I have JAXB preserve nulls when receiving a JSON sting that contains

  • 0

How do I have JAXB preserve nulls when receiving a JSON sting that contains a null or “” value.

String:{"id":null,"fname":"John","region":""}

returns Object:

 Person {
    Integer id = 0
    String fname = "John"
    Region regions = 0 }

I would like it to return null instead of 0

Here is what I have so far:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
    private JAXBContext context;
    private Class<?>[] types = {Person.class};

    public JAXBContextResolver() throws Exception {
        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
    }

    public JAXBContext getContext(Class<?> objectType) {
        for (Class<?> c : types) {
            if (c.equals(objectType)) {
                return context;
            }
        }
        return null;
    }
}

Person.class is annotated with @XmlRootElement I’ve tried looking into Jackson annotations but have had no success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:06:34+00:00Added an answer on June 3, 2026 at 9:06 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    Below is an example of how this can be done with MOXy.

    Person

    MOXy will unmarshal the Person object as you want without specifying any metadata. To have the output JSON contain null values, you can use the @XmlElement(nillable=true) annotation (see Binding to JSON & XML – Handling Null).

    package forum8748537;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Person {
    
        @XmlElement(nillable=true)
        Integer id;
    
        @XmlElement(nillable=true)
        String fname;
    
        @XmlElement(nillable=true)
        Region regions;
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB (JSR-222) provider you need to add a file called jaxb.properties in the same package as your domain classes with the following entry (see Specifying EclipseLink MOXy as Your JAXB Provider).

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    package forum8748537;
    
    import java.io.StringReader;
    import java.util.*;
    
    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(2);
            properties.put("eclipselink.media-type", "application/json");
            properties.put("eclipselink.json.include-root", false);
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties);
    
            StringReader json = new StringReader("{\"id\":null,\"fname\":\"John\",\"region\":\"\"}");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Person person = unmarshaller.unmarshal(new StreamSource(json), Person.class).getValue();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(person, System.out);
        }
    
    }
    

    Output

    {
       "id" : null,
       "fname" : "John",
       "regions" : null
    }
    

    Using MOXy in a JAX-RS Application

    For an example of using MOXy in a JAX-RS application see:

    • MOXy as Your JAX-RS JSON Provider – Server Side
    • MOXy as Your JAX-RS JSON Provider – Client Side
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object graph that contains a cycle. How do I get JAXB
The scenario: I have a servlet that receives xmls, parses them (using JAXB), persists
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
I have a problem with Jaxb enumerations that is related to this bug https://jaxb.dev.java.net/issues/show_bug.cgi?id=708
I am using JAXB to marshall. I have noticed that when I am marshaling
Suppose I have a JAXB element like this: @XmlElement private double value; this will
I have an application that is running Jaxb 2.1.12. I am running JDK 1.5
I have a web service (built using jaxb/jaxws) that invokes a stateless EJB to
I have a simple restful service that transforms a JAXB-anntotated beans to response XML
I have a service that needs to generate xml. Currently I am using jaxb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.