is it an anti-pattern to use more than one method for sending information in a RESTful POST

不想你离开。 提交于 2019-12-25 07:15:12

问题


This SO answer thoroughly goes through all possible methods of passing and accessing parameters in a RESTful POST method.

Is it an anti-pattern to use more than one method?

For instance would you consider the following method that passes a JSON object in the content body of the POST HTTP method and also uses a path parameter an anti-pattern?

@POST
@Path("/modifyPerson/{idx}")
@Consumes(MediaType.APPLICATION_JSON)
public Response modifyPerson(Person newPerson, @PathParam("idx") int i) {
    // ...
}

I could create a richer class (e.g. PersonWithIdx) that combines both Person and the idx integer parameter and pass that instead with no need to resort to path parameters. The only thing the above code buys me is that it obviates the need to create this extra class. But is it sound?


回答1:


Is it an anti-pattern to use more than one method?

No.

For instance would you consider the following method that passes a JSON object in the content body of the POST HTTP method and also uses a path parameter an anti-pattern?

No.

If you have a large number of resources that share the same backing implementation, then using a UriTemplate to identify how those messages consume a particular HTTP request is appropriate.

I could create a richer class (e.g. PersonWithIdx) that combines both Person and the idx integer parameter and pass that instead with no need to resort to path parameters. The only thing the above code buys me is that it obviates the need to create this extra class. But is it sound?

Taking identifiers out of the URI to pack them into the request body sounds more like RPC than it sounds like REST.



来源:https://stackoverflow.com/questions/38573530/is-it-an-anti-pattern-to-use-more-than-one-method-for-sending-information-in-a-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!