I use CustomServletServerHttpResponse to inherit ServletServerHttpResponse and customize the compression algorithm for response requests. However, I found that my close method cannot be called due to the following code Unable to clear CustomOutputStream native memory,

https://github.com/spring-projects/spring-framework/blob/6d97791f5e82da8babbeac8bce5693cc56a78161/spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java#L452-L456

I have searched for relevant issues, but I have not found a clear solution to close my own defined OutputStream

Comment From: brucelwl

Using java.lang.ref.Cleaner in Java 9 seems to be a good solution ?

  @Override
    public OutputStream getBody() throws IOException {
        if (customOutputStream == null) {
            OutputStream body = super.getBody();
            customOutputStream = new CustomOutputStream(body);
            cleanable = cleaner.register(this, new CustomOutputStreamClear(customOutputStream));
        }
        return customOutputStream;
    }