What's the best way to suppress a runtime cons

2020-06-01 07:40发布

I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always getting a message written to the console at runtime:

WARNING: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

In the code I have to write the response to a byte array anyway, so it is the getResponseBody() method that I ought to use. But is there an easy way that I can suppress the warning message so I don't have to look at it at every run?

If it was a compiler error, I'd use the @SuppressWarnings annotation, but this isn't a compile-time issue; it happens at runtime. Also, I could use getResponseBodyAsStream to write to a ByteArrayOutputStream, but this seems like a hacky way to get around the warning (extra lines of code to do what getResponseBody() is already doing for me).

My guess is that the answer involves System.out or System.err manipulation, but is there a good way to do this?

7条回答
甜甜的少女心
2楼-- · 2020-06-01 08:26

Is the library outputting through log4j? if so, editing the log4j.properties to set the output for this class to "ERROR" would work, e.g.

log4j.logger.org.apache.commons.httpclient.methods.PostMethod=ERROR
查看更多
劫难
3楼-- · 2020-06-01 08:27

that warning happends when httpClient have no idea about the length of the return data you should set the content-length attribute in your server end

response.addHeader("Content-Type", "text/html; charset=utf-8");
response.addHeader("Content-Length", String.valueOf(output.getBytes("utf8").length));

after that, that warning should disapear

查看更多
【Aperson】
4楼-- · 2020-06-01 08:30

This issue has already been debated on the ASF JIRA. There are two ways to resolve this:

  • Set a higher value for BUFFER_WARN_TRIGGER_LIMIT. A high enough value is more likely to suppress the warning; however, that defeats the purpose of the warning itself. If you are buffering a lot of data to be eventually parsed in one pass, you are better off reading the data from a stream into an array before working on the array.
  • Set the logging level to a higher value for HttpClient, if you are comfortable with ERROR or FATAL.
查看更多
Deceive 欺骗
5楼-- · 2020-06-01 08:31

If you want to cleanly stop this log entry, there's a tunable max that triggers the warning. I saw this looking at the code.

        int limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);
        if ((contentLength == -1) || (contentLength > limit)) {
            LOG.warn("Going to buffer response body of large or unknown size. "
                    +"Using getResponseBodyAsStream instead is recommended.");
        }

HttpMethodBase.setParams() looks like the place to set HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT to the desired value.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-06-01 08:38

Assuming the warning is written to stderr, you can always suppress the warning by piping stderr to /dev/null, or whatever the equivalent on your system is.

查看更多
放荡不羁爱自由
7楼-- · 2020-06-01 08:43

to simply 'save' the stderr messages then print them after completion of the main task

PrintStream origErr = System.err;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream newErr = new PrintStream(baos);
System.setErr(newErr);

====== do stuff ======

System.setErr(origErr);
System.err.print(baos);
查看更多
登录 后发表回答