在Spring中处理POST请求的REST方法究竟如何工作? [英] How exactly works this REST method that handle a POST request in Spring?

查看:120
本文介绍了在Spring中处理POST请求的REST方法究竟如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在攻读Spring Core认证,我对Spring MVC中的RESTful webapp *练习有一些疑问。

I am studying for the Spring Core certification and I have some doubt related to an exercise on **RESTful webapp* in Spring MVC.

因此,在示例中,我有以下创建新帐户对象的方法

So, in the example, I have the following method that create a new Account object

/**
 * 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());
}

我知道:


  1. @RequestMapping 注释,在这种情况下,指定此方法处理 POST HttpRequest朝向 / accounts 资源。我知道它使用 POST 请求,因为根据REST样式,POST动词表示必须创建新资源。

  1. @RequestMapping annotation, in this case, specify that this method handle POST HttpRequest towards the /accounts resource. I know that it use the POST request because according to the REST style the POST "verbs" means that a new resource have to be created.

我认为这个注释:

 @ResponseStatus(HttpStatus.CREATED)

表示当方法正确结束时(当 HttpResponse 发送到客户端时),它将 201 创建)进入HttpResponse状态字段。所以它指定新对象的创建没有问题。它是真的还是我错过了什么?

means that when the method correctly end (when the HttpResponse is send to the client) it put the 201 (CREATED) into the HttpResponse status field. So it specify that the creation of the new object is gone ok. Is it true or am I missing something?

该方法的第一个参数是:

The first parameter of the method is:

@RequestBody Account newAccount

阅读文档在我看来此参数绑定到Web请求的主体。请求的主体通过HttpMessageConverter传递,以根据请求的内容类型解析方法参数。

Reading the documentation it seems to me that this parameter is bounded to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request.

那么,究竟是什么意思?我认为这意味着进入我的HttpRequest的 body 我的JSON格式的Account对象,并使用Jackson将其转换为经典的帐户 Java对象。是对还是我错过了什么?

So, what exactly means? I think that it means that into the body of my HttpRequest I have my Account object in JSON format and that it is used Jackson to convert it into a classic Account Java object. Is it right or am I missing something?

该方法的第二个参数是:

The second parameter of the method is:

@Value(#{request.requestURL})StringBuffer url

@Value("#{request.requestURL}") StringBuffer url

究竟是什么意思?

然后该方法将获得的对象保存在数据库中。

Then the method save the obtained object on the database.

最后返回:

return entityWithLocation(url, account.getEntityId());

但究竟是什么意思?什么回来了?在哪里?结果不是HttpResponse?

but what exactly means? what is returning? and where? the result is not into the HttpResponse?

编辑1:

entityWithLocation()方法定义在前一个方法的同一个类中,这是它的代码:

The entityWithLocation() method is definied in the same class of the previous method and this is its code:

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);
}


推荐答案

以下是我对你的理解问题。

Below is my understanding on your question.




@RequestBody Account newAccount

@RequestBody Account newAccount



您的理解是正确的,关于此点。

Your understanding is correct,regarding this point.



  1. @Value(#{request.requestURL})StringBuffer url


它等同于request.getRequestURL();请参阅 API

It is equivalent to request.getRequestURL(); See API



  1. entityWithLocation(url,account.getEntityId())


根据此方法中的代码。
返回 HttpEntity Object,表示http请求或响应实体(,包括标题和正文),在你的情况下,它是响应
在方法中,您已添加资源(帐户)位置

As per code inside this method. It returns HttpEntity Object,which represent http request or response entity(which includes headers and body),in your case it is response. Inside the method,you have added the location of resource(account) created.

这篇关于在Spring中处理POST请求的REST方法究竟如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆