1

考虑示例:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.File;
import java.io.FileInputStream;

public class Exmaple {
    public static void main (String[] args) throws Exception {

        Object object = new Object() {
            private String id = "someId";
        };

        final JsonGenerator generator = new JsonFactory().createGenerator(System.out);
        generator.writeStartObject();
        generator.writeFieldName("data");
        generator.writeBinary(new FileInputStream(new File("D:\\!Temp\\elasticsearch\\pdf.pdf")), -1);
        generator.writeEndObject();
        generator.writeObject(object);
        generator.close();
    }
}

目标是object在 json 流式传输期间添加所有字段。使用该代码,我得到:

{
  "data": "JVBERi0xLjMKJ="
} {
  "id": "someId",
}

如何告诉jackson不要{}从对象写?

更新

预期的结果是(例如data+ 来自 java 对象的所有键):

{
  "data": "JVBERi0xLjMKJ=",
  "id": "someId",
}
4

1 回答 1

0

只是不要打电话writeEndObject()后打电话writeBinary它应该看起来像:

final JsonGenerator generator = new JsonFactory().createGenerator(System.out);
        generator.writeStartObject();
        generator.writeFieldName("data");
        generator.writeBinary(new FileInputStream(new File("D:\\!Temp\\elasticsearch\\pdf.pdf")), -1);
        generator.writeObject(object);
        generator.writeEndObject();
        generator.close();
于 2017-09-29T09:45:38.940 回答