问题描述
我正在使用 Jersey 构建一个非常简单的 REST API,我的日志文件中有一个我不确定的警告.
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.
警告:一个 servlet POST 请求,URIhttp://myserver/mycontext/myapi/users/12345?action=delete,包含表单参数请求正文,但请求正文有被 servlet 或servlet 过滤器访问请求参数.仅资源方法使用@FormParam 将作为预期的.资源方法消耗请求体通过其他方式将没有按预期工作.
我的 webapp 只定义了 Jersey servlet,映射到/myapi/*
My webapp only has the Jersey servlet defined, mapped to /myapi/*
如何停止这些警告?
推荐答案
对我来说,警告是针对 POST application/x-www-form-urlencoded 显示的.我正在使用 Spring Boot,它有一个 HiddenHttpMethodFilter,它在做其他任何事情之前先执行 getParameter ......所以我最终做了这个讨厌的覆盖:
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);
}
}
};
}
这篇关于如何修复 Jersey POST 请求参数警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!