Jersey POJOMappingFeature to convert null to empty string?

℡╲_俬逩灬. 提交于 2019-12-06 20:09:29
Ben Green

If you are using Jackson, you can write a custom serializer for your pojos. For instance:

public class FooSerializer extends JsonSerializer<Foo> {
    @Override
    public void serialize(final Foo value, final JsonGenerator jgen,
        final SerializerProvider provider) throws IOException
    {
        if (value.bar() == null) {
            jgen.writeString("");
        } else {
            jgen.writeString(value.bar());
        }
    }
}

Then, on your Foo object:

@JsonSerialize(using = FooSerializer.class)
public class Foo {
    private String bar;

    public String bar() {
        return bar;
    }
}

You can also create custom deserializers by extending JsonDeserializer to handle incoming nulls, or put a filter in place to parse the body?

If you are using Jackson and have got access to ObjectMapper you can configure it in a way you want.

You can set a null value serializer that writes empty strings for null objects, and register a String deserializer which treats empty strings in JSON as nulls.

Here is an example:

public class JacksonNull {

    public static class Bean {
        public String field1;
        public String field2;

        public Bean() {}

        public Bean(String field1, String field2) {
            this.field1 = field1;
            this.field2 = field2;
        }

        @Override
        public String toString() {
            return "Bean{" +
                    "field1='" + field1 + '\'' +
                    ", field2='" + field2 + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) throws IOException {
        Bean bean = new Bean(null, "value");
        ObjectMapper mapper = new ObjectMapper();
        mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value,
                                  JsonGenerator jgen,
                                  SerializerProvider provider) throws IOException {
                jgen.writeString("");
            }
        });
        SimpleModule module = new SimpleModule();
        module.addDeserializer(String.class, new JsonDeserializer<String>() {
            @Override
            public String deserialize(JsonParser jp,
                                      DeserializationContext ctxt) throws IOException {
                String string = jp.getValueAsString();
                return string.isEmpty() ? null : string;
            }
        });
        mapper.registerModule(module);
        String json = mapper.writeValueAsString(bean);
        System.out.println(json);
        System.out.println(mapper.readValue(json, Bean.class));
    }
}

Output:

{"field1":"","field2":"value"}
Bean{field1='null', field2='value'}

From Jersey maillist:

One more thing: to ignore the null values, you will probably also need to provide your own Jackson ObjectMapper provider with the Jackson SerializationConfig.Feature WRITE_NULL_PROPERTIES [3] set to false. Please see [2] for details on how to hook your custom ObjectMapper into Jersey.

HTH,

~Jakub

[2]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.6-ea06/jacksonjsonprovider-1.6-ea06-project.zip [3]http://jackson.codehaus.org/1.7.0/javadoc/org/codehaus/jackson/map/SerializationConfig.Feature.html#WRITE_NULL_PROPERTIES

I think this is your answer.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!