How to fix Jersey POST request parameters warning?

后端 未结 9 904
暗喜
暗喜 2020-12-08 06:50

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,

相关标签:
9条回答
  • 2020-12-08 07:12

    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

    0 讨论(0)
  • 2020-12-08 07:17

    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)
    
    0 讨论(0)
  • 2020-12-08 07:18

    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"/>
    
    0 讨论(0)
  • 2020-12-08 07:21

    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.

    0 讨论(0)
  • 2020-12-08 07:23

    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.

    0 讨论(0)
  • 2020-12-08 07:25

    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;
        }
    });
    
    0 讨论(0)
提交回复
热议问题