我不断从任何使用 APPLICATION_FORM_URLENCODED 表单数据的 POST 操作中收到这些警告消息:
A servlet request to the URI (local request URI) contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
我已经将其追溯到 org.glassfish.jersey.servlet.WebComponent:
if (!form.asMap().isEmpty()) {
containerRequest.setProperty(InternalServerProperties.FORM_DECODED_PROPERTY, form);
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, LocalizationMessages.FORM_PARAM_CONSUMED(containerRequest.getRequestUri()));
}
}
因此,如果有任何表单数据,它将始终打印此警告。我是不是做错了什么,因为用正常操作的警告填充日志似乎不是一个好主意。
最佳答案
我将 Logback 与 Slf4j 一起使用,您只需添加“jul-to-slf4j”作为依赖项并在 logback.xml 中配置它。
在 pom.xml 中:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
要将 jul 消息重定向到 logback,请在 logback.xml 中添加:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<appender name="OUT" class="ch.qos.logback.core.ConsoleAppender">
...
<logger name="org.glassfish.jersey.servlet" level="ERROR" />
</configuration>
将 org.glassfish.jersey.servlet 的级别设置为 ERROR 可以避免日志文件中出现警告消息。
关于java - 来自 Jersey 2.x 的过多警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808594/