简体   繁体   English

使用POST创建新资源的REST方法

[英]REST method to Create new resource using POST

  1. I want to create a new resource using POST at /users { “name” : “Mr. 我想在/ users {“名称”:“先生。 Woods”, “age” : 29 } 伍兹”,“年龄”:29}
  2. I do not have ID in the request. 我的请求中没有ID。

  3. The server would create a new resource with an auto-generated ID (even the same data exists with a different ID which is what it is supposed to do), say 1234 服务器将使用自动生成的ID创建新资源(即使同一数据存在,但ID也不同),例如1234

  4. The new resource location /users/1234 has to be returned in the response 必须在响应中返回新的资源位置/ users / 1234

  5. Now, should I just return the new ID as return value in the response or the new ID set in the input request and return the whole entity? 现在,我是否应该仅将新ID作为响应中的返回值或输入请求中设置的新ID返回并返回整个实体? { “id” : 1234, “name” : “Mr. {“ id”:1234,“ name”:“ Mr。 Woods”, “age” : 29 } 伍兹”,“年龄”:29}

  6. Also, when a request hits POST /users but already have an ID provided in the input, do we need to validate for this being null before creating a new one? 另外,当一个请求到达POST / users但已经在输入中提供了ID时,我们是否需要在创建一个新请求之前验证它是否为null? What would be the Http response code in this case when an ID exists in the input? 如果输入中存在ID,在这种情况下,Http响应代码是什么?

I would use spring hateoas ( https://spring.io/projects/spring-hateoas ) and I would create an object like this: 我将使用spring hateoas( https://spring.io/projects/spring-hateoas )并创建一个像这样的对象:

public class User extends ResourceSupport {
    private Long id;
    private int age;
    private String name;
    //Setter and getter and eventually hashCode and equals
}

then in a controller I would a method like this: 然后在控制器中,我将采用以下方法:

@RequestMapping(value="/users", method={RequestMethod.POST}, produces="application/json")
public ResponseEntity<User> managePerson(@RequestBody User p)
{
  if(p.getId() != null)
  {
    //Update; in this case a 204 http status is enough and no content is required
   Return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
  }
  else
  {
   //Save and return the saved objecty with the returned ID and set the new location
   p.add(new Link("/users/"+p.getId()));
   Return ResponseEntity.status(HttpStatus.OK).body(p);
  }

}

Now, should I just return the new ID as return value in the response or the new ID set in the input request and return the whole entity? 现在,我是否应该仅将新ID作为响应中的返回值或输入请求中设置的新ID返回并返回整个实体? { “id” : 1234, “name” : “Mr. {“ id”:1234,“ name”:“ Mr。 Woods”, “age” : 29 } 伍兹”,“年龄”:29}

A Rest POST request that is successful doesn't need necessarily to have a response body, overall if these are exactly the same data provided in the request + the created ID. 成功的Rest POST请求不一定需要具有响应正文,总的来说,如果这些完全是请求中提供的相同数据+创建的ID。
A 201 Created response that contains in the location header the URI of the created resource seems fine. location header中包含所创建资源的URI的201 Created响应看起来不错。

It doesn't mean that it is forbidden to set a body in the POST response, just that you should set it only if it adds some added value for the client. 这并不意味着禁止在POST响应中设置正文,仅当它为客户端添加了一些附加值时才应设置它。
For example suppose that the resource creation may change the data provided by the client (cleaning, computations, and so for...), it could make sense to set the resource in the body response. 例如,假设资源创建可能会更改客户端提供的数据(清理,计算等),那么在主体响应中设置资源可能是有意义的。

Also, when a request hits POST /users but already have an ID provided in the input, do we need to validate for this being null before creating a new one? 另外,当一个请求到达POST / users但已经在输入中提供了ID时,我们是否需要在创建一个新请求之前验证它是否为null? What would be the Http response code in this case when an ID exists in the input? 如果输入中存在ID,在这种情况下,Http响应代码是什么?

It depends on the way which you want that your Rest services behave : 这取决于您希望Rest服务的行为方式:

  • Either you decide that updating is legal with POST : in this case if the ID is null , you create the resource and if it is not null , you update the resource and if all is fine you could return 202 Accepted . 无论是你决定的更新是法律与POST :在该ID是否这种情况下null ,创建资源,如果它不是null ,您更新资源,如果一切都很好,你可以返回202 Accepted
  • Or you decide that updating is not legal with POST : in this other case if the ID is not null , you return a error response such as 400 Bad Request . 或者,您决定使用POST更新是不合法的:在另一种情况下,如果ID不为null ,则会返回错误响应,例如400 Bad Request

I do not have ID in the request. 我的请求中没有ID。

You DO NOT need one. 您不需要一个。 You can simply use the Db-SDK for this. 您只需为此使用Db-SDK。

The new resource location /users/1234 has to be returned in the response 必须在响应中返回新的资源位置/ users / 1234

It would be better to make it hypermedia driven system HATEOS 最好使其成为超媒体驱动的系统HATEOS

Now, should I just return the new ID as return value in the response or the new ID set in the input request and return the whole entity? 现在,我是否应该仅将新ID作为响应中的返回值或输入请求中设置的新ID返回并返回整个实体? { “id” : 1234, “name” : “Mr. {“ id”:1234,“ name”:“ Mr。 Woods”, “age” : 29 } 伍兹”,“年龄”:29}

Like I said its good to return the complete hypermedia info, so that the clients can refer them without any further modification to make any restful requests 就像我说的那样,返回完整的超媒体信息是很好的,这样客户端可以无需进一步修改即可引用它们,以发出任何轻松的请求

{
    "id": 1234,
    "name": "Mr.Woods",
    "age": 29,
    "address": "/1234/address",
    "some-param": "/path-to-the-resource"
}

Also, when a request hits POST /users but already have an ID provided in the input, do we need to validate for this being null before creating a new one? 另外,当一个请求到达POST / users但已经在输入中提供了ID时,我们是否需要在创建一个新请求之前验证它是否为null?

Ideally, you should validate it if you are following HTTP specs. 理想情况下,如果遵循HTTP规范,则应对其进行验证。 Post should always result in the creation of resource. 发布应始终导致资源的创建。 however it's just about following specification nothing stops you if you wish to make your POST idempotent 但是,这只是遵循规范,如果您希望使POST幂等,则不会阻止您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM