这个在 Spring 中处理 POST 请求的 REST 方法到底是如何工作的?
How exactly works this REST method that handle a POST request in Spring?
我正在学习 Spring 核心认证,我对 Spring MVC 中 **RESTful webapp* 的练习有一些疑问。
因此,在示例中,我使用以下方法创建新的 Account 对象
/**
* Creates a new Account, setting its URL as the Location header on the
* response.
*/
@RequestMapping(value = "/accounts", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public HttpEntity<String> createAccount(@RequestBody Account newAccount,
@Value("#{request.requestURL}") StringBuffer url) {
Account account = accountManager.save(newAccount);
return entityWithLocation(url, account.getEntityId());
}
我知道:
@RequestMapping注解,在本例中,指定该方法处理POSTHttpRequest /accounts 资源。我知道它使用 POST 请求,因为根据 REST 样式 POST "verbs" 意味着必须创建新资源。
我认为这个注解:
@ResponseStatus(HttpStatus.CREATED)
意味着当方法正确结束时(当 HttpResponse 被发送到客户端时)它把 201 (CREATED) 到 HttpResponse 状态字段。所以它指定新对象的创建已经成功。是真的还是我遗漏了什么?
方法的第一个参数是:
@RequestBody Account newAccount
在我看来,阅读文档时,此参数已绑定到 Web 请求的主体。请求的主体通过 HttpMessageConverter 传递,以根据请求的内容类型解析方法参数。
那么,究竟是什么意思呢?我认为这意味着在我的 HttpRequest 的 body 中,我的 Account 对象采用 JSON 格式,并且使用 Jackson 将其转换为 class ic 帐户 Java 对象。是对的还是我遗漏了什么?
方法的第二个参数是:
@Value("#{request.requestURL}") StringBuffer url
具体是什么意思?
然后该方法将获取的对象保存到数据库中。
终于return:
return entityWithLocation(url, account.getEntityId());
但究竟是什么意思? returning 是什么?在哪里?结果是没有进入HttpResponse?
编辑 1:
entityWithLocation() 方法在与上一个方法相同的 class 中定义,这是它的代码:
private HttpEntity<String> entityWithLocation(StringBuffer url,
Object resourceId) {
// Configure and return an HttpEntity object - it will be used to build
// the HttpServletResponse
HttpHeaders headers = new HttpHeaders();
headers.setLocation(getLocationForChildResource(url, resourceId));
return new HttpEntity<String>(headers);
}
以下是我对你的问题的理解。
@RequestBody Account newAccount
关于这一点,你的理解是正确的。
- @Value("#{request.requestURL}") StringBuffer url
相当于request.getRequestURL();参见 API
- entityWithLocation(url, account.getEntityId())
根据此方法中的代码。
它是 returns HttpEntity
对象,表示 http 请求或响应实体(which includes headers and body
),在你的例子中是 response
。
在该方法中,您添加了 location
of resource(account)
created.
我正在学习 Spring 核心认证,我对 Spring MVC 中 **RESTful webapp* 的练习有一些疑问。
因此,在示例中,我使用以下方法创建新的 Account 对象
/**
* Creates a new Account, setting its URL as the Location header on the
* response.
*/
@RequestMapping(value = "/accounts", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public HttpEntity<String> createAccount(@RequestBody Account newAccount,
@Value("#{request.requestURL}") StringBuffer url) {
Account account = accountManager.save(newAccount);
return entityWithLocation(url, account.getEntityId());
}
我知道:
@RequestMapping注解,在本例中,指定该方法处理POSTHttpRequest /accounts 资源。我知道它使用 POST 请求,因为根据 REST 样式 POST "verbs" 意味着必须创建新资源。
我认为这个注解:
@ResponseStatus(HttpStatus.CREATED)
意味着当方法正确结束时(当 HttpResponse 被发送到客户端时)它把 201 (CREATED) 到 HttpResponse 状态字段。所以它指定新对象的创建已经成功。是真的还是我遗漏了什么?
方法的第一个参数是:
@RequestBody Account newAccount
在我看来,阅读文档时,此参数已绑定到 Web 请求的主体。请求的主体通过 HttpMessageConverter 传递,以根据请求的内容类型解析方法参数。
那么,究竟是什么意思呢?我认为这意味着在我的 HttpRequest 的 body 中,我的 Account 对象采用 JSON 格式,并且使用 Jackson 将其转换为 class ic 帐户 Java 对象。是对的还是我遗漏了什么?
方法的第二个参数是:
@Value("#{request.requestURL}") StringBuffer url
具体是什么意思?
然后该方法将获取的对象保存到数据库中。
终于return:
return entityWithLocation(url, account.getEntityId());
但究竟是什么意思? returning 是什么?在哪里?结果是没有进入HttpResponse?
编辑 1:
entityWithLocation() 方法在与上一个方法相同的 class 中定义,这是它的代码:
private HttpEntity<String> entityWithLocation(StringBuffer url,
Object resourceId) {
// Configure and return an HttpEntity object - it will be used to build
// the HttpServletResponse
HttpHeaders headers = new HttpHeaders();
headers.setLocation(getLocationForChildResource(url, resourceId));
return new HttpEntity<String>(headers);
}
以下是我对你的问题的理解。
@RequestBody Account newAccount
关于这一点,你的理解是正确的。
- @Value("#{request.requestURL}") StringBuffer url
相当于request.getRequestURL();参见 API
- entityWithLocation(url, account.getEntityId())
根据此方法中的代码。
它是 returns HttpEntity
对象,表示 http 请求或响应实体(which includes headers and body
),在你的例子中是 response
。
在该方法中,您添加了 location
of resource(account)
created.