Java Spring MVC和X-HTTP-Method-Override参数

Java Spring MVC和X-HTTP-Method-Override参数,java,spring,http,spring-mvc,Java,Spring,Http,Spring Mvc,我使用的是Spring MVC,我有一个更新用户配置文件的功能: @RequestMapping(value = "/{userName}" + EndPoints.USER_PROFILE, method = RequestMethod.PUT) public @ResponseBody ResponseEntity<?> updateUserProfile( @PathVariable String userName, @RequestBody UserProfi

我使用的是Spring MVC,我有一个更新用户配置文件的功能:

@RequestMapping(value = "/{userName}" + EndPoints.USER_PROFILE,
    method = RequestMethod.PUT)
public @ResponseBody ResponseEntity<?> updateUserProfile(
    @PathVariable String userName, @RequestBody UserProfileDto userProfileDto) {
    // Process update user's profile
} 
@RequestMapping(value=“/{userName}”+EndPoints.USER\u PROFILE,
method=RequestMethod.PUT)
public@ResponseBody ResponseEntity updateUserProfile(
@PathVariable字符串用户名,@RequestBody UserProfileDto UserProfileDto){
//进程更新用户的配置文件
} 
我已经开始使用JMeter,出于某种原因,他们在发送带有主体的PUT请求时遇到问题(在请求主体中或使用请求参数hack)

我知道,在Jersey中,您可以添加一个过滤器来处理X-HTTP-Method-Override请求参数,这样您就可以发送POST请求并使用header参数覆盖它

在SpringMVC中有什么方法可以做到这一点吗

谢谢

Spring MVC具有允许您包含请求参数(
\u method
)以覆盖http方法的。您只需要将过滤器添加到web.xml中的过滤器链中


我不知道有现成的解决方案可以使用
X-HTTP-Method-Override
头,但您可以自己创建一个类似于
hiddenhttmpmethodfilter
的筛选器,它使用头更改值而不是请求参数。

您可以将此类用作筛选器:

public class HttpMethodOverrideHeaderFilter extends OncePerRequestFilter {
  private static final String X_HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override";

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {
    String headerValue = request.getHeader(X_HTTP_METHOD_OVERRIDE_HEADER);
    if (RequestMethod.POST.name().equals(request.getMethod()) && StringUtils.hasLength(headerValue)) {
      String method = headerValue.toUpperCase(Locale.ENGLISH);
      HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
      filterChain.doFilter(wrapper, response);
    }
    else {
      filterChain.doFilter(request, response);
    }
  }

  private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
    private final String method;

    public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
      super(request);
      this.method = method;
    }

    @Override
    public String getMethod() {
      return this.method;
    }
  }
}
资料来源: