【发布时间】:2020-06-14 17:06:35
【问题描述】:
通常,我们将请求主体作为控制器方法的参数。 Spring 将主体绑定到变量的类型。
我希望请求正文作为控制器的属性,以便其他私有方法可以访问它。
public class UserController {
private String body; // Request body should automatically bind to this String.
private HttpServletRequest request;
@Autowired
public UserController(HttpServletRequest request) {
this.request = request;
}
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> create(@RequestBody String body) {
// I know I can do like: this.body = body.
// But don't want to do that.
}
private someMethod() {
// Give me access of request body please....
}
【问题讨论】:
标签: spring spring-boot spring-mvc spring-restcontroller