简体   繁体   中英

JAXB null instead empty string during marshaling

How I can print 'null' as field value, when marshalling the string?

Example: error and error_code are Strings, and i want to use 'null' as a value indicating that there is no value/errors happened on the server side.

{
   "error_code": null,
   "error": null
}

Today, I have to use EMPTY values, so that "error_code" or "error" these fields generally fall into json, and if they were not explicitly initialized as this.errorCode = StringUtils.EMPTY; So today, I have next json:

{
   "error_code": "",
   "error": ""
}

This is how that looks in a code:

@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class Response
{
        @SuppressWarnings("unused")
        private static final Logger log = LoggerFactory.getLogger(Response.class);

        public static final String ERROR_FIELD_NAME = "error";
        public static final String ERROR_CODE_FIELD_NAME = "error_code";

        // @XmlJavaTypeAdapter(CafsResponse.EmptyStringAdapter.class)
        @XmlElement(name = Response.ERROR_CODE_FIELD_NAME)
        private String errorCode;

        // @XmlJavaTypeAdapter(CafsResponse.EmptyStringAdapter.class)
        @XmlElement(name = Response.ERROR_FIELD_NAME)
        private String errorMessage;

    // Empty Constructor
    public Response()
    {
            this.errorCode = StringUtils.EMPTY; // explicit initialization, otherwise error_code will not appear as part of json, how to fix this this ?
            this.errorMessage = StringUtils.EMPTY;
    }

etc...

// Empty Constructor
public Response()
{
        this.errorCode = null; // this variant dosn't work either, and error_code again didn't get to json 
        this.errorMessage = null;
}

See, @XmlJavaTypeAdapter, i thought that this potentially could help me - but no :)

Instead of null value, i'm getting "null" as string.

if (StringUtils.isEmpty(str))
{
   return null;
}
return str;

{
   "error_code": "null", // this is not whta i wanted to get.
   "error": "null"
}

Any help on this? - ask me if something is not clear.

full list:

/**
 * Empty string Adapter specifying how we want to represent empty strings
 * (if string is empty - treat it as null during marhsaling)
 * 
 */
@SuppressWarnings("unused")
private static class EmptyStringAdapter extends XmlAdapter<String, String>
{

        @Override
        public String unmarshal(String str) throws Exception
        {
                return str;
        }


        @Override
        public String marshal(String str) throws Exception
        {
                if (StringUtils.isEmpty(str))
                {
                        return null;
                }
                return str;
        }

}

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

You could use MOXy as your JSON provider to support this use case. Below is an example:

Response

MOXy will marshal properties marked with @XmlElement(nillable=true) to the representation you are looking for (see: http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html ).

package forum11319741;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {

        public static final String ERROR_FIELD_NAME = "error";
        public static final String ERROR_CODE_FIELD_NAME = "error_code";

        @XmlElement(name = Response.ERROR_CODE_FIELD_NAME, nillable = true)
        private String errorCode;

        @XmlElement(name = Response.ERROR_FIELD_NAME, nillable = true)
        private String errorMessage;

}

jaxb.properties

To use MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ):

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

Demo

package forum11319741;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Response.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);

        Response response = new Response();
        marshaller.marshal(response, System.out);
    }

}

Output

{
   "error_code" : null,
   "error" : null
}

MOXy and JAX-RS

You can use the MOXyJsonProvider class to enable MOXy as your JSON provider in your JAX-RS application (see: http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html ).

package org.example;

import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;

public class CustomerApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(2);
        set.add(MOXyJsonProvider.class);
        set.add(CustomerService.class);
        return set;
    }

}

For More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM