因此,我使用@RestController
注释了我的课程,而不是我经常使用的@Controller
。我的乐趣......
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = Paths.SOMEPATH, method = RequestMethod.POST)
public MyObject createMyObject(){
MyObject myObject = newMyObject();
myObjectRepository.save(myObject);//autowired repository
return myObject;
}
制作405方法不允许(整个方法通过,返回时)
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = Paths.SOMEPATH, method = RequestMethod.POST)
public @ResponseBody MyObject createMyObject(){
MyObject myObject = newMyObject();
myObjectRepository.save(myObject);//autowired repository
return myObject;
}
工作得很好我得到了我期待的东西
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = Paths.SOMEPATH, method = RequestMethod.POST)
public MyObject createMyObject(){
MyObject myObject = newMyObject();
return myObjectRepository.save(myObject);//autowired repository
}
这也很有用,这就是为什么我很困惑。有人可以解释原因吗?
(我删除了@PathVariable
之类的内容以及myObject
和@RequestBody
对象上的操作,用于填充MyObject
实例中的数据以简洁起见)
编辑: 机构405响应
{
"timestamp": 1446735218918,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/myPath"
}
答案 0 :(得分:-2)
在Http中,错误4XX是用户错误。这意味着您没有正确地调用您的服务。
"status": 405,
"error": "Method Not Allowed",
这个错误意味着你没有调用正确的Http方法,你定义了像POST这样的方法,可能你是用GET调用它。