在向spring-boot发出POST请求时,如何在java方法中获得有效负载



我想看看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。

最新更新