我想看看POST请求有什么,但是,我不想在方法头中这样做,而是使用方法体中的java代码。如果POST请求为null,我希望返回一些值(比如在使用try-catch时(,而不是使该方法失败。
以下是如何在Spring中完成:
@RestController
@RequestMapping(value = "/some-path")
public class YourController {
@RequestMapping(value = "/further path")
@RequestMapping(method = RequestMethod.POST)
public YourReturnObject postMethod(@RequestBody DataYouWant dataYouWant) {
// Basically, dataYouWant is the request body
}
}
如果没有严格的@RequestBody
格式,可以简单地使用Map<String, ?>
将json值转换为Map
而不是DataYouWant
对象。很明显,YourReturnObject
是您希望作为响应体返回的对象,它将串行化为json。