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

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

问题描述

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

因此,在示例中,我使用以下方法创建一个新的 Account 对象

/*** 创建一个新帐户,将其 URL 设置为* 回复.*/@RequestMapping(value = "/accounts", method = RequestMethod.POST)@ResponseStatus(HttpStatus.CREATED)公共 HttpEntitycreateAccount(@RequestBody Account newAccount,@Value("#{request.requestURL}") StringBuffer url) {Account account = accountManager.save(newAccount);返回 entityWithLocation(url, account.getEntityId());}

我知道:

  1. @RequestMapping 注释,在这种情况下,指定此方法处理面向 /accounts 资源的 POST HttpRequest.我知道它使用 POST 请求,因为根据 REST 风格,POST动词"意味着必须创建一个新资源.

  2. 我认为这个注释:

     @ResponseStatus(HttpStatus.CREATED)

    意味着当方法正确结束时(当 HttpResponse 被发送到客户端时)它会将 201 (CREATED) 放入HttpResponse 状态字段.所以它指定新对象的创建没有问题.这是真的还是我遗漏了什么?

  3. 该方法的第一个参数是:

    @RequestBody 账户 newAccount

    阅读文档,我觉得这个参数被限制在 web 请求的正文中.请求的正文通过 HttpMessageConverter 传递,以根据请求的内容类型解析方法参数.

    那么,究竟是什么意思?我认为这意味着在我的 HttpRequest 的 body 中,我有 JSON 格式的 Account 对象,并且使用 Jackson 将其转换为经典的 Account Java 对象.是对的还是我遗漏了什么?

  4. 该方法的第二个参数是:

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

    究竟是什么意思?

  5. 然后该方法将获取的对象保存到数据库中.

  6. 终于回来了:

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

    但究竟是什么意思?什么是返回?在哪里?结果没进HttpResponse?

编辑 1:

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

私有HttpEntityentityWithLocation(StringBuffer url,对象资源 ID) {//配置并返回一个 HttpEntity 对象 - 它将用于构建//HttpServletResponseHttpHeaders headers = new HttpHeaders();headers.setLocation(getLocationForChildResource(url, resourceId));返回新的 HttpEntity(headers);}

解决方案

以下是我对您的问题的理解.

<块引用>

  1. @RequestBody 帐户新帐户

关于这一点,您的理解是正确的.

<块引用>

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

相当于 request.getRequestURL();请参阅API

<块引用>

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

根据此方法中的代码.它返回 HttpEntity 对象,代表 http 请求或响应实体( 包括头和正文),在你的情况下它是 response.在方法内部,您添加了创建的 resource(account)location.

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

I know that:

  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.

  2. I think that this annotation:

     @ResponseStatus(HttpStatus.CREATED)
    

    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?

  3. The first parameter of the method is:

    @RequestBody Account newAccount
    

    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.

    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?

  4. The second parameter of the method is:

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

    What exactly means?

  5. Then the method save the obtained object on the database.

  6. Finally it return:

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

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

EDIT 1:

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.

  1. @RequestBody Account newAccount

Your understanding is correct,regarding this point.

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

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

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

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天全站免登陆