我想看看 POST 请求有什么,但是,我不想在方法头中做,而是在方法体中使用 java 代码。如果 POST 请求为空,我想返回一些值(例如使用 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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句