我发送 POST 请求
$.ajax({
type: "POST",
url: "/common/ajax/advert/",
data: data,
dataType: "json",
contentType: "application/json",
success: function(r){}
});
到 Controller
@Controller
@RequestMapping(value = "/common/ajax/advert")
public class Controller {
@RequestMapping(value="/", method=RequestMethod.POST)
@ResponseBody
public Map<String,Object> adsSearch(@RequestBody Map<String,Object> data){
Map<String,Object> result = new HashMap<String,Object>();
List<Advert> ads = advSrv.getAds(data);
result.put("obj", ads);
return result;
}
并返回 404 错误,但在 spring 3.2 中这工作正常。
具有 RequestMethod.GET 的 Controller 在新旧版本中都能正常工作。
请帮我解决这个问题。
UPD.1 我尝试使用 RequestMethod.POST 创建一个 @RestController 类(Spring 4.1) - 但它也不起作用。
UPD.2 在日志中,Spring 正确映射了此方法,但未处理 post 请求(与 get 请求不同,它们工作正常)。
最佳答案
最后我发现了一个问题——升级到4.0后的Spring Security,默认启用了csrf保护:
DEBUG: org.springframework.security.web.csrf.CsrfFilter - Invalid CSRF token found for post-request
我添加了对csrf保护的支持并且一切正常。
关于java - 从 3.2 迁移到 4.1 后,Spring MVC 中的 POST 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29619647/