如何在Restlet中注册Jackson Jdk8模块

时间:2015-05-18 12:40:55

标签: json jackson java-8 restlet-2.0

是否可以在Restlet jackson-datatype-jdk8扩展包中注册org.restlet.ext.jackson模块?我需要利用新的Optional功能。我的猜测是它应该可以通过转换器服务(getConverterService())访问,但是我在文档中找不到任何可以确定如何设置模块的内容。

2 个答案:

答案 0 :(得分:5)

我最终从各种来源拼凑出一个与Restlet 2.3一起使用的答案。我的猜测是,这将被重构为Restlet 3,并且它不适用于版本< 2.3请注意,此解决方案很可能具有有限的保质期。

第一步是创建一个自定义Jackson转换器,它实现您拥有的任何自定义要求:

public class CustomJacksonConverter extends JacksonConverter { 
    @Override 
    protected <T> JacksonRepresentation<T> create(MediaType mediaType, T source) { 
        ObjectMapper mapper = createMapper(); 
        JacksonRepresentation<T> jr = new JacksonRepresentation<T>(mediaType, source); 
        jr.setObjectMapper(mapper); 
        return jr; 
    } 

    @Override 
    protected <T> JacksonRepresentation<T> create(Representation source, Class<T> objectClass) { 
        ObjectMapper mapper = createMapper(); 
        JacksonRepresentation<T> jr = new JacksonRepresentation<T>(source, objectClass); 
        jr.setObjectMapper(mapper); 
        return jr; 
    } 


    private ObjectMapper createMapper() { 
        JsonFactory jsonFactory = new JsonFactory(); 
        jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); 
        ObjectMapper mapper = new ObjectMapper(jsonFactory); 
        mapper.registerModule(new Jdk8Module()); 
        return mapper; 
    } 

} 

然后,您需要创建一种用新副本替换默认Jackson转换器的方法:

static void replaceConverter(
            Class<? extends ConverterHelper> converterClass,
            ConverterHelper newConverter) {

        ConverterHelper oldConverter = null;

        List<ConverterHelper> converters = Engine.getInstance().getRegisteredConverters();
        for (ConverterHelper converter : converters) {
            if (converter.getClass().equals(converterClass)) {
                converters.remove(converter);
                oldConverter = converter;
                break;
            }
        }

        converters.add(newConverter);
    }

您现在可以替换入站根目录中的转换器:

replaceConverter(JacksonConverter.class, new CustomJacksonConverter());

答案 1 :(得分:1)

@tarka的开端很棒,但是给了我错误的信息;我提出了一些[影响较小]的改进措施:

public class CustomJacksonConverter extends JacksonConverter {

    @Override
    protected <T> JacksonRepresentation<T> create(MediaType mediaType, T source) {
        return new CustomJacksonRepresentation<>(mediaType, source);
    }

    @Override
    protected <T> JacksonRepresentation<T> create(Representation source, Class<T> objectClass) {
        return new CustomJacksonRepresentation<>(source, objectClass);
    }

}

然后......

public class CustomJacksonRepresentation<T> extends JacksonRepresentation<T> {

    public CustomJacksonRepresentation(MediaType mediaType, T object) {
        super(mediaType, object);
    }

    public CustomJacksonRepresentation(Representation representation, Class<T> objectClass) {
        super(representation, objectClass);
    }

    public CustomJacksonRepresentation(T object) {
        super(object);
    }

    @Override
    protected ObjectMapper createObjectMapper() {
        ObjectMapper mapper = super.createObjectMapper();
        mapper.registerModule(new Jdk8Module());
        return mapper;
    }

}

并以与@tarka相同的方式替换转换器