is it an anti-pattern to use more than one method

2019-09-14 09:16发布

问题:

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.