Rest 为什么删除请求中需要@Responsebody,而不是POST、PUT或GET中需要@Responsebody?
在控制器类中,我有3种不同的方法处理来自POST、PUT和DELETE HTTP方法的请求:Rest 为什么删除请求中需要@Responsebody,而不是POST、PUT或GET中需要@Responsebody?,rest,http,spring-mvc,Rest,Http,Spring Mvc,在控制器类中,我有3种不同的方法处理来自POST、PUT和DELETE HTTP方法的请求: @RequestMapping(value = "/path1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ReturnDataType insertRequestDataType(@RequestBody RequestDataType requestData) throws Se
@RequestMapping(value = "/path1", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ReturnDataType insertRequestDataType(@RequestBody RequestDataType requestData) throws ServiceException {
//Some code here to call service class and do something with requestData
return new ReturnDataType();
}
@RequestMapping(value = "/path2", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ReturnDataType updateRequestDataType(@RequestBody RequestDataType requestData) throws ServiceException {
//Some code here to call service class and do something with requestData
return new ReturnDataType();
}
@RequestMapping(value = "path3/{arg1}/path4/{arg2}", method = RequestMethod.DELETE)
public @ResponseBody ReturnDataType deleteRequestDataType(@PathVariable String arg1,
@PathVariable String arg2) throws ServiceException {
//Some code here to call service class and do something with arg1 and arg2
return new ReturnDataType();
}
前两种方法在返回类型中没有@ResponseBody
注释的情况下工作得非常好,但最后一种方法不是。如果从第三个方法中删除注释,将出现以下错误:
{
"success": false,
"cause": "Request method 'DELETE' not supported",
"message": "Request Error"
}
为什么会发生这种情况
提前感谢您的时间。因此,如果您删除
@ResponseBody
,您的deleteRequestDataType
方法实际上已执行,但该错误是在方法返回时发生的?Spring MVC本身不会创建该响应。您还使用了哪些其他框架?只有Spring,但我正在使用Postman for Chrome来测试这些请求调用,http响应显示在Postman中。@Reno no,实际上方法没有执行,我设置了一个断点以确保方法被命中,但请求从未到达方法本身。当我添加@RequestBody
时,执行删除requestdatatype
方法。