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

时间:2010-01-06 09:14:05

标签: rest post warnings jersey

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

  

警告:一个servlet POST请求,到   URI   http://myserver/mycontext/myapi/users/12345?action=delete,   包含表单中的表单参数   请求正文,但请求正文   已经被servlet消耗掉了   servlet过滤器访问请求   参数。只有资源方法   使用@FormParam将起作用   预期。消耗的资源方法   请求机构将通过其他方式   没有按预期工作。

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

如何停止这些警告?

9 个答案:

答案 0 :(得分:12)

对我来说,警告显示为POST application / x-www-form-urlencoded。我正在使用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);
                }
            }
        };
    }

答案 1 :(得分:10)

此消息旨在警告开发人员已经使用了请求实体主体这一事实,因此任何其他尝试读取消息正文的操作都将失败。

可以安全地忽略该消息或将其从日志中过滤掉:

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 :(得分:6)

以下thread描述了您收到的警告。听起来好像你的web.xml中定义了一个在Jersey之前处理请求的过滤器。

答案 3 :(得分:3)

我刚刚将JQuery中的ajax函数设置为contentType: "application/x-www-form-urlencoded; charset=UTF-8",因为使用先前的解决方案(没有Jersey)我遇到了一些编码问题。当我删除时,消息消失了,一切正常。

答案 4 :(得分:3)

最后通过确保我的请求标头中有 Content-Type:application / json 来解决这个问题(显然,在客户端)

答案 5 :(得分:2)

此警告是WebComponent唯一记录的内容,因此只需将日志记录更改为ERROR级别或关闭logback.xml中的此组件的日志记录或配置日志记录的任何位置。您不需要编写自定义过滤器来忽略此特定消息,因为此组件中没有记录其他消息。

来自org.glassfish.jersey.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()));
            }
        }

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

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.

在logback.xml中关闭WebComponent的注销,如下所示:

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

答案 6 :(得分:1)

右。 所以我一直在遭遇这个问题,我一直试图以不同的方式解决它,但我不想改变我的web.xml设置,只是因为如果我用Postman测试我的应用程序它工作得很完美,但是当它与webapp集成时,它会因上述问题(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.

而失败

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

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

和voilá,警告已经消失了。

由于

答案 7 :(得分:0)

在我的情况下,当我在方法中将对象日期更改为字符串时,我已修复此错误。

错误:

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

固定

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

答案 8 :(得分:0)

将其放入您的资源签名。或者,如果使用@PUT或@POST,则在您的项目中找到此字符串,已经有人使用过。这应该有帮助

import javax.ws.rs.Consumes;

@Consumes(MediaType.APPLICATION_JSON)