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

bra*_*ter 36 rest post warnings jersey

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

警告:对URI http:// myserver/mycontext/myapi/users/12345?action = delete的servlet POST请求 包含请求正文中的表单参数,但servlet或servlet过滤器已使用请求正文请求参数.只有使用@FormParam的资源方法才能按预期工作.通过其他方式使用请求主体的资源方法将无法按预期工作.

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

我怎么能停止这些警告?

小智 13

对我来说,警告显示POST应用程序/ 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);
                }
            }
        };
    }
Run Code Online (Sandbox Code Playgroud)

  • 如果您根本不需要`HiddenHttpMethodFilter`,则可以将其禁用。参见[here](/sf/answers/1989970811/)如何做到这一点! (2认同)

Iul*_*oiu 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;
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 我认为你应该尝试解决问题而不是隐藏它.我的2美分(我也可能是错的). (5认同)

Ale*_*ton 6

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

  • 这一定是坏事吗?有没有办法隐藏控制台/日志输出? (2认同)

小智 6

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