How can I exclude null fields form my JSON request in Jersey?

雨燕双飞 提交于 2019-12-06 02:35:22
Tomas Bartalos

I beleive, that jersey is using jackson for serialization. For excluding null fields from serialized json, try to annotate the target class with @JsonInclude(Include.NON_NULL). As explained in this post.

If you can't change entities, you must configure your custom ObjectMapper:

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {

    final ObjectMapper mapper;

    public MyObjectMapperProvider() {
        mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper
    }
}

then register your custom provider to the client:

Client client = ClientBuilder
    .newClient()
    .register(MyObjectMapperProvider.class)
    .register(JacksonFeature.class);

its described here

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