Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这个REST方法在Spring中如何处理POST请求?_Java_Spring_Rest_Spring Mvc_Annotations - Fatal编程技术网

Java 这个REST方法在Spring中如何处理POST请求?

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

我正在学习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)
@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中

  • 编辑1:

    entityWithLocation()方法在前面方法的同一类中定义,其代码如下:

    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(标头); }
    以下是我对您问题的理解

  • @请求主体帐户newAccount

  • 关于这一点,你的理解是正确的

  • @值(“#{request.requestURL}”)字符串缓冲区url
  • 它相当于request.getRequestURL();看

  • entityWithLocation(url,account.getEntityId())
  • 按照此方法中的代码。 它返回
    HttpEntity
    对象,该对象表示http请求或响应实体(
    ,其中包括头和正文
    ),在您的示例中,它是
    response

    在方法内部,您已经添加了创建的
    资源(帐户)
    位置。

    entityWithLocation方法在哪里定义?你能发布它的实现吗?@Tyrionlanister编辑了我的原始帖子,添加了所需的信息