考虑示例:
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",
}