如何修复Jersey POST请求参数警告?

时间:2021-09-03 19:34:29

I'm building a very simple REST API using Jersey, and I've got a warning in my log files that I'm not sure about.

我正在使用Jersey构建一个非常简单的REST API,并且在我的日志文件中有一个我不确定的警告。

WARNING: A servlet POST request, to the URI http://myserver/mycontext/myapi/users/12345?action=delete, 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.

警告:向URI http://myserver/mycontext/myapi/users/12345?action=delete,在请求体中包含表单参数,但是请求体已经被servlet或servlet筛选器使用,访问请求参数。只有使用@FormParam的资源方法才能正常工作。以其他方式使用请求主体的资源方法将不能正常工作。

My webapp only has the Jersey servlet defined, mapped to /myapi/*

我的webapp只定义了Jersey servlet,映射到/myapi/*

How can I stop these warnings?

我如何停止这些警告?

8 个解决方案

#1


8  

This message is meant to warn developers about the fact that the request entity body has been consumed, thus any other attempts to read the message body will fail.

此消息的目的是警告开发人员请求实体主体已经被使用,因此任何其他读取消息主体的尝试都将失败。

It is safe to ignore the message or filter it out from the logs:

忽略该消息或将其从日志中过滤出来是安全的:

java.util.logging.Logger jerseyLogger =
        java.util.logging.Logger.getLogger(WebComponent.class.getName());
jerseyLogger.setFilter(new Filter() {
    @Override
    public boolean isLoggable(LogRecord record) {
        boolean isLoggable = true;
        if (record.getMessage().contains("Only resource methods using @FormParam")) {
            isLoggable = false;
        }
        return isLoggable;
    }
});

#2


7  

For me the warning was showing for POST application/x-www-form-urlencoded. And I am using Spring Boot which has an HiddenHttpMethodFilter that does a getParameter before anything else... So I ended up doing this nasty override:

对我来说,这个警告显示的是POST application/ www-form- urlencoding。我使用的是Spring Boot,它有一个HiddenHttpMethodFilter,它在任何事情之前都做一个getParameter…所以我最终做了这个恶心的重写:

@Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                    FilterChain filterChain) throws ServletException, IOException {
                if ("POST".equals(request.getMethod())
                        && request.getContentType().equals(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
                    filterChain.doFilter(request, response);
                } else {
                    super.doFilterInternal(request, response, filterChain);
                }
            }
        };
    }

#3


5  

The following thread describes the warning you are receiving. It sounds as though you might have a filter defined in your web.xml that is processing the request before Jersey does.

下面的线程描述您正在接收的警告。听起来好像您的web中定义了一个过滤器。在Jersey之前处理请求的xml。

#4


3  

I just had my ajax-function in JQuery set to contentType: "application/x-www-form-urlencoded; charset=UTF-8" because with a prior solution (without Jersey) I had some encoding problems. When I removed that the message was gone and everything worked fine.

我刚刚将JQuery中的ajax函数设置为contentType:“application/ www-form- urlencoding;charset=UTF-8“因为有一个以前的解决方案(没有Jersey),我有一些编码问题。当我删除这条消息时,一切都很顺利。

#5


3  

Finally got rid of this by making sure I had Content-Type: application/json in my request headers (obviously, on the client side)

最后,通过确保在请求头(显然是在客户端)中有Content-Type: application/json,消除了这个问题。

#6


2  

This warning is the only thing the WebComponent logs, so just turn logging up to ERROR level or turn off logging for this component in your logback.xml or wherever you have logging configured. You don't need to write a custom filter to ignore this specific message since there are no other messages logged from this component.

此警告是WebComponent记录的惟一内容,因此只需将日志记录调到错误级别或在您的logback中关闭该组件的日志记录。xml或任何配置了日志记录的地方。您不需要编写自定义过滤器来忽略此特定的消息,因为此组件没有其他消息。

Source code snippet from org.glassfish.jersey.servlet.WebComponent version 2.14:

来自org. glassfish.jerse.servlet的源代码片段。WebComponent 2.14版本:

        if(!form.asMap().isEmpty()) {
            containerRequest.setProperty("jersey.config.server.representation.decoded.form", form);
            if(LOGGER.isLoggable(Level.WARNING)) {
                LOGGER.log(Level.WARNING, LocalizationMessages.FORM_PARAM_CONSUMED(containerRequest.getRequestUri()));
            }
        }

The localized message that is used for this warning message is:

用于此警告消息的本地化消息是:

form.param.consumed=A servlet request to the URI {0} 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.

Turn logging off for the WebComponent in your logback.xml like so:

在您的回退中关闭WebComponent的注销。xml一样:

<logger name="org.glassfish.jersey.servlet.WebComponent" level="OFF" additivity="false"/>

#7


1  

Right. So I've been suffering this issue, and I've been trying to solve it on different ways, but I did't want to change my web.xml settings, just because if I was testing my application with Postman it worked perfect, but when it was being integrated with the webapp it fails with the mentioned issue (A servlet request to the URI {MY_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.)

正确的。所以我一直在忍受这个问题,我一直试图用不同的方式来解决它,但是我不想改变我的网络。xml设置,因为如果我测试的应用程序与邮递员工作完美,但当它被集成与应用不能提到的问题(servlet请求URI { MY_URI }包含请求在请求主体形式参数的身体已经被访问请求的servlet或servlet筛选器参数。只有使用@FormParam的资源方法才能正常工作。以其他方式使用请求主体的资源方法将不会像预期的那样工作。

So as @clijk mentioned, you only have to set your headers as:

所以正如@clijk提到的,你只需要将你的标题设置为:

"Content-Type":"application/json"
"charset":"UTF-8"

and voilá, the warning it's gone.

瞧,警告已经不见了。

Thanks

谢谢

#8


0  

In my case I've fixed this error when I've changed the Object Date to String in the method.

在我的例子中,当我将对象日期更改为方法中的字符串时,我修正了这个错误。

Error:

错误:

@POST
@Path("/myPath")
@Produces(MediaType.APPLICATION_JSON)
public List<MyObject> myMethod(@FormParam("StartDate") Date date) throws Exception {

Fixed

固定

@POST
@Path("/myPath")
@Produces(MediaType.APPLICATION_JSON)
public List<MyObject> myMethod(@FormParam("StartDate") String date) throws Exception {

#1


8  

This message is meant to warn developers about the fact that the request entity body has been consumed, thus any other attempts to read the message body will fail.

此消息的目的是警告开发人员请求实体主体已经被使用,因此任何其他读取消息主体的尝试都将失败。

It is safe to ignore the message or filter it out from the logs:

忽略该消息或将其从日志中过滤出来是安全的:

java.util.logging.Logger jerseyLogger =
        java.util.logging.Logger.getLogger(WebComponent.class.getName());
jerseyLogger.setFilter(new Filter() {
    @Override
    public boolean isLoggable(LogRecord record) {
        boolean isLoggable = true;
        if (record.getMessage().contains("Only resource methods using @FormParam")) {
            isLoggable = false;
        }
        return isLoggable;
    }
});

#2


7  

For me the warning was showing for POST application/x-www-form-urlencoded. And I am using Spring Boot which has an HiddenHttpMethodFilter that does a getParameter before anything else... So I ended up doing this nasty override:

对我来说,这个警告显示的是POST application/ www-form- urlencoding。我使用的是Spring Boot,它有一个HiddenHttpMethodFilter,它在任何事情之前都做一个getParameter…所以我最终做了这个恶心的重写:

@Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        return new HiddenHttpMethodFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                    FilterChain filterChain) throws ServletException, IOException {
                if ("POST".equals(request.getMethod())
                        && request.getContentType().equals(MediaType.APPLICATION_FORM_URLENCODED_VALUE)) {
                    filterChain.doFilter(request, response);
                } else {
                    super.doFilterInternal(request, response, filterChain);
                }
            }
        };
    }

#3


5  

The following thread describes the warning you are receiving. It sounds as though you might have a filter defined in your web.xml that is processing the request before Jersey does.

下面的线程描述您正在接收的警告。听起来好像您的web中定义了一个过滤器。在Jersey之前处理请求的xml。

#4


3  

I just had my ajax-function in JQuery set to contentType: "application/x-www-form-urlencoded; charset=UTF-8" because with a prior solution (without Jersey) I had some encoding problems. When I removed that the message was gone and everything worked fine.

我刚刚将JQuery中的ajax函数设置为contentType:“application/ www-form- urlencoding;charset=UTF-8“因为有一个以前的解决方案(没有Jersey),我有一些编码问题。当我删除这条消息时,一切都很顺利。

#5


3  

Finally got rid of this by making sure I had Content-Type: application/json in my request headers (obviously, on the client side)

最后,通过确保在请求头(显然是在客户端)中有Content-Type: application/json,消除了这个问题。

#6


2  

This warning is the only thing the WebComponent logs, so just turn logging up to ERROR level or turn off logging for this component in your logback.xml or wherever you have logging configured. You don't need to write a custom filter to ignore this specific message since there are no other messages logged from this component.

此警告是WebComponent记录的惟一内容,因此只需将日志记录调到错误级别或在您的logback中关闭该组件的日志记录。xml或任何配置了日志记录的地方。您不需要编写自定义过滤器来忽略此特定的消息,因为此组件没有其他消息。

Source code snippet from org.glassfish.jersey.servlet.WebComponent version 2.14:

来自org. glassfish.jerse.servlet的源代码片段。WebComponent 2.14版本:

        if(!form.asMap().isEmpty()) {
            containerRequest.setProperty("jersey.config.server.representation.decoded.form", form);
            if(LOGGER.isLoggable(Level.WARNING)) {
                LOGGER.log(Level.WARNING, LocalizationMessages.FORM_PARAM_CONSUMED(containerRequest.getRequestUri()));
            }
        }

The localized message that is used for this warning message is:

用于此警告消息的本地化消息是:

form.param.consumed=A servlet request to the URI {0} 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.

Turn logging off for the WebComponent in your logback.xml like so:

在您的回退中关闭WebComponent的注销。xml一样:

<logger name="org.glassfish.jersey.servlet.WebComponent" level="OFF" additivity="false"/>

#7


1  

Right. So I've been suffering this issue, and I've been trying to solve it on different ways, but I did't want to change my web.xml settings, just because if I was testing my application with Postman it worked perfect, but when it was being integrated with the webapp it fails with the mentioned issue (A servlet request to the URI {MY_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.)

正确的。所以我一直在忍受这个问题,我一直试图用不同的方式来解决它,但是我不想改变我的网络。xml设置,因为如果我测试的应用程序与邮递员工作完美,但当它被集成与应用不能提到的问题(servlet请求URI { MY_URI }包含请求在请求主体形式参数的身体已经被访问请求的servlet或servlet筛选器参数。只有使用@FormParam的资源方法才能正常工作。以其他方式使用请求主体的资源方法将不会像预期的那样工作。

So as @clijk mentioned, you only have to set your headers as:

所以正如@clijk提到的,你只需要将你的标题设置为:

"Content-Type":"application/json"
"charset":"UTF-8"

and voilá, the warning it's gone.

瞧,警告已经不见了。

Thanks

谢谢

#8


0  

In my case I've fixed this error when I've changed the Object Date to String in the method.

在我的例子中,当我将对象日期更改为方法中的字符串时,我修正了这个错误。

Error:

错误:

@POST
@Path("/myPath")
@Produces(MediaType.APPLICATION_JSON)
public List<MyObject> myMethod(@FormParam("StartDate") Date date) throws Exception {

Fixed

固定

@POST
@Path("/myPath")
@Produces(MediaType.APPLICATION_JSON)
public List<MyObject> myMethod(@FormParam("StartDate") String date) throws Exception {