Java 如何创建json运行时并将其值传递给使用gzip的compress方法

Java 如何创建json运行时并将其值传递给使用gzip的compress方法,java,json,compression,jackson,Java,Json,Compression,Jackson,我想压缩json对象运行时。(jackkon+Gzip) 我想压缩生成运行时的json文件,使用 public static String compress(String str) throws IOException { if (str == null || str.length() == 0) { return str; } System.out.println("String length : " + str.length()); ByteA

我想压缩json对象运行时。(jackkon+Gzip)

我想压缩生成运行时的json文件,使用

public static String compress(String str) throws IOException {
    if (str == null || str.length() == 0) {
        return str;
    }
    System.out.println("String length : " + str.length());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    String outStr = out.toString("ISO-8859-1");
    System.out.println("Output String lenght : " + outStr.length());
    return outStr;
 }
如何创建json文件运行时并将其传递给compress方法?用于压缩,因为objectMapper.writeValue返回void

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue("???", getTransaction());

压缩工作在字节上,而不是字符串上。您不能盲目地使用
getBytes()
,然后盲目地转换回字符串。@LouisWasserman应该在哪里写入值?在objectMapper.writeValue中。我有马克????