Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Jersey POST request parameters warning?

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.

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.

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

How can I stop these warnings?

like image 273
brabster Avatar asked Jan 06 '10 09:01

brabster


5 Answers

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:

@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);
                }
            }
        };
    }
like image 171
javo Avatar answered Nov 20 '22 05:11

javo


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;
    }
});
like image 26
Iulian Ghionoiu Avatar answered Nov 20 '22 04:11

Iulian Ghionoiu


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.

like image 6
Alex Winston Avatar answered Nov 20 '22 05:11

Alex Winston


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

like image 5
Arnold B. Avatar answered Nov 20 '22 06:11

Arnold B.


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.

like image 4
cljk Avatar answered Nov 20 '22 04:11

cljk