This post was helpful in showing how to set a custom JSONProvider both with and 
without Spring:


http://stackoverflow.com/a/6344047

I'm assuming custom providers are registered before or override the built-in 
ones so they get to inspect things first?

Here's some test cases explaining what I'm trying to do:


@Test // passes
public void JSONProviderIncludesNil() throws Exception {
    JSONProvider p = new JSONProvider();
    Widget widget = new Widget();
    widget.setName("Hello World");
    String json = toJson(p, widget, "widget", Widget.class);
    assertEquals("{\"widget\":{\"id\":{\"@xsi.nil\":\"true\"},\"name\":\"Hello 
World\"}}", json);   
}

@Test // fails
public void OmitNillJSONProviderExcludesNil() throws Exception {
    JSONProvider p = new OmitNillJSONProvider();
    Widget widget = new Widget();
    widget.setName("Hello World");
    String json = toJson(p, widget, "widget", Widget.class);
    assertEquals("{\"widget\":{\"name\":\"Hello World\"}}", json);   
}    

private <T> String toJson(JSONProvider p, T item, String root, Class<T> 
classOfT) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();        
    p.writeTo(
        new JAXBElement<T>(new QName("", root), classOfT, item), // hack ???    
        
        classOfT, classOfT, 
        classOfT.getAnnotations(), 
        MediaType.APPLICATION_JSON_TYPE, 
        new MetadataMap<String, Object>(), 
        stream);
    return stream.toString(); 
}
    
// yes, I know XmlRootElement is missing
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "widget", propOrder = {"id","name"})
public static class Widget {        
    @XmlElement(nillable = true) // can't change
    protected Integer id;
    @XmlElement(nillable = true) // can't change
    protected String name;        
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }    
}

public class OmitNillJSONProvider extends JSONProvider {
    // ???
}

Maybe I should bypass all this jaxb annotation sillyness and just let gson 
handle serializing the object as if it didn't have any annotations? 



________________________________
 From: Ron Grabowski <[email protected]>
To: "[email protected]" <[email protected]> 
Sent: Saturday, April 14, 2012 10:17 PM
Subject: JAX-RS in v2.5.x: how to set custom JSONProvider(?) without Spring to 
stop {"@xsi.nil":"true"}
 
I have objects with fields setup like this:

 @XmlElement(nillable = true)
 protected Integer id;

that I send out via JAX-RS with @Produces(MediaType.APPLICATION_JSON). The json 
payload ends up like this when the fields aren't set:


 "id":{"@xsi.nil":"true"}

I can't change the @XmlElement on the field because another part of my system 
depends on it. I want to either output:

 "id" : null

or ideally not output that field at all in the json payload. I think I need 
extend the built in JSONProvder and tweak it (and/or Jettison) a little bit. 


How can I do that without Spring? Can someone explain a solution both in terms 
of web.xml and JAXRSServerFactoryBean (unit testing)?

Thanks,
Ron

Reply via email to