使用yaml factory创建objectMapper时,可以设置几个配置参数:

   ObjectMapper o = new ObjectMapper(new YAMLFactory());
    // o.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    // o.enable(SerializationFeature.CLOSE_CLOSEABLE);

问题是在YAML Generator中将忽略此配置:
 @Override
    public void close() throws IOException
    {
        if (!isClosed()) {
            _emitter.emit(new DocumentEndEvent(null, null, false));
            _emitter.emit(new StreamEndEvent(null, null));
            super.close();
            _writer.close();
        }
    }

即使在javadoc中写了别的东西

最佳答案

使用YAML映射器而不是对象映射器。这对我来说很好。

YAMLMapper yamlMapper = new YAMLMapper();
yamlMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
yamlMapper.configure(YAMLGenerator.Feature.WRITE_DOC_START_MARKER, true);
yamlMapper.enable(SerializationFeature.CLOSE_CLOSEABLE);

关于java - 为什么YAMLGenerator不关闭流取决于配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50445012/

10-10 19:16