How to fix Jersey POST request parameters warning?
Asked Answered
B

10

43

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?

Barbaraanne answered 6/1, 2010 at 9:14 Comment(3)
I've answered this question here <https://mcmap.net/q/391014/-excessive-warning-messages-from-jersey-2-x>. Hope it helps!Deration
Not seeing a warning does not mean you fixed the issue behind itHornbill
If you're using Spring Boot, here is another workaroundNullifidian
B
14

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);
                }
            }
        };
    }
Budgerigar answered 25/5, 2016 at 11:5 Comment(3)
Solved my problems too! (Is this a very special (stupid) setup we have that brought us into this issue, or why does other people not run into this issue?!?Garter
I get this error when using Jersey MVC. If I switch to Spring MVC, this override is not necessary.Garter
If you don't need the HiddenHttpMethodFilter at all, you could disabled it. See here how to do that!Unprintable
L
12

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;
    }
});
Leuko answered 8/7, 2014 at 14:26 Comment(4)
Should be placed somewhere in the initialisation part of you application, before you start serving requests.Leuko
I think that you should try to solve the issue instead of hiding it. My 2 cents (I might be wrong also).Abracadabra
Why would that be safe to ignore!? Its clearly a problem as your code doesnt get it... If you are expecting to do something with the data, you clearly have a problem. Do not just ignore it.... @Abracadabra is right. Burying you head in the sand and pretending you dont have a problem is stupid.Krol
It makes sense to ignore in a public API scenario where you can't control the funny things your API consumers might do when working on their integration, and you have too many of them to deal with the log noise.Peta
K
6

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.

Kiyokokiyoshi answered 21/5, 2010 at 18:28 Comment(1)
Is that necessarily a bad thing? Is there a way to just hide the console/log output?Uncertainty
D
6

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

Dispatcher answered 10/11, 2015 at 17:52 Comment(1)
This would be a better answer if you could explain why we need this instead of application/x-www-form-urlencoded.Reproach
W
4

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.

Wing answered 30/7, 2013 at 14:5 Comment(0)
A
2

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.)

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

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

and voilá, the warning it's gone.

Thanks

Abracadabra answered 29/1, 2015 at 13:58 Comment(0)
D
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.

Source code snippet from org.glassfish.jersey.servlet.WebComponent version 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:

<logger name="org.glassfish.jersey.servlet.WebComponent" level="OFF" additivity="false"/>
Democrat answered 16/2, 2015 at 22:54 Comment(0)
V
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 {
Vaunt answered 25/9, 2015 at 14:32 Comment(0)
L
0

Put this to your resource signature. Or find this string in your project someone already use this if @PUT or @POST is used. This should help

import javax.ws.rs.Consumes;

@Consumes(MediaType.APPLICATION_JSON)
Landowska answered 21/10, 2019 at 10:4 Comment(0)
P
0

This problem seem has been fixed in version 2.38
#5208

Pretypify answered 18/11, 2023 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.