Jackson 2.0 with Jersey 1.12

这一生的挚爱 提交于 2019-12-05 13:40:48

Custom provider works; and "official" Jackson 2.0 JSON provider project does the same, with bit more features (ability to use @JsonView annotation and a few others on resource methods).

This is one of nice things with JAX-RS: everything is modular, and adding new improved providers is very easy.

I've Jackson 2.0 and Jersey 1.12 in my project. I had not any problems with it, but probably reason is that I had custom Provider with some additional ObjectMapper settings. Simplified version:

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;

@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

    private final ObjectMapper defaultObjectMapper;

    public ObjectMapperProvider() {
        defaultObjectMapper = new ObjectMapper();
    }

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

If you're trying to use JsonView with Jersey, you must use org.codehaus.jackson.map.annotate.JsonView if you use the method 2.2 on here: http://wiki.fasterxml.com/JacksonFAQJaxRs

If you want to use JsonView from com.fasterxml, you must use the general method (1) on that page.

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