Java 这个REST方法在Spring中如何处理POST请求?
我正在学习Spring核心认证,我对Spring MVC中的**RESTful webapp*练习有一些疑问 因此,在本例中,我使用以下方法创建一个新的帐户对象Java 这个REST方法在Spring中如何处理POST请求?,java,spring,rest,spring-mvc,annotations,Java,Spring,Rest,Spring Mvc,Annotations,我正在学习Spring核心认证,我对Spring MVC中的**RESTful webapp*练习有一些疑问 因此,在本例中,我使用以下方法创建一个新的帐户对象 /** * Creates a new Account, setting its URL as the Location header on the * response. */ @RequestMapping(value = "/accounts", method = RequestMethod.POST) @ResponseS
/**
* 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());
}
表示当方法正确结束时(当HttpResponse被发送到客户端时),它将201(创建的)放入HttpResponse状态字段。因此,它指定新对象的创建已完成。这是真的还是我遗漏了什么
@RequestBody Account newAccount
在阅读文档时,我觉得这个参数被绑定到web请求的主体。请求主体通过HttpMessageConverter传递,以根据请求的内容类型解析方法参数
那么,到底是什么意思?我认为这意味着,在HttpRequest的主体中,我有一个JSON格式的Account对象,并使用它将其转换为经典的AccountJava对象。这是对的还是我遗漏了什么@RequestBody Account newAccount
@值(“#{request.requestURL}”)字符串缓冲区url
到底是什么意思return entityWithLocation(url, account.getEntityId());
但到底是什么意思?什么东西回来了?在哪里?结果不在HttpResponse中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);
}
私有HttpEntity entityWithLocation(StringBuffer url,
对象资源ID){
//配置并返回一个HttpEntity对象-它将用于生成
//HttpServletResponse
HttpHeaders=新的HttpHeaders();
setLocation(getLocationForChildResource(url,resourceId));
返回新的HttpEntity(标头);
}
以下是我对您问题的理解
HttpEntity
对象,该对象表示http请求或响应实体(,其中包括头和正文
),在您的示例中,它是response
。
在方法内部,您已经添加了创建的
资源(帐户)
的位置。entityWithLocation方法在哪里定义?你能发布它的实现吗?@Tyrionlanister编辑了我的原始帖子,添加了所需的信息