Preferred method for a REST style url?

I am creating a web application that includes REST style services, and I wanted to get some clarification regarding the preferred (standard) method of how POST requests should be accepted on the Java server side:

Method 1: http: // localhost: 8080 / services / processser / uid / {uidvalue} / eid / {eidvalue}

Method 2: http: // localhost: 8080 / services / processuser {uid: ", eid:" "} - this will be sent as JSON to the message body

Both methods will use the content type "application / json", but are there any advantages, disadvantages for each method. One of the drawbacks of method 2, I can immediately think that JSON data should be mapped to a Java object, thus creating a Java object at any time when any user accesses the "processuser" api servlet process. Your input is greatly appreciated.

In this particular case, the data will be used to query the database to return a json response to the client.

+4
source share
2 answers

I think we need to get back a bit from your question. Your path segment begins with:

/services/processuser 

This is mistake. The URI should identify the resource, not the operation. This may not always be possible, but it is something that you should strive for.

In this case, you seem to identify your user with uid and eid (whatever it is). You could create paths, for example, the user is /user/<uid>/<eid> , /user/<uid>-<eid> (if you must /user/uid/<uid>/eid/<eid> ); if eid is a specialization and not on par with uid , then /user/<uid>;eid=<eid> would be more appropriate.

You could create new users by sending messages /user/ or /user/<uid>/<eid> if you knew the identifiers in advance, deleting users with DELETE to /user/<uid>/<eid> and change the state , using PUT on /user/<uid>/<eid> .

To answer your question, you should use PUT on /user/<uid>/<eid> if "processuser" seeks to change the state of the user with the data you provided. Otherwise, the comparison with the REST model is not so clean, perhaps the best option would be to define the resource /user/process/<uid>/<eid> and POST with all the data there, but the POST before /user/process with all the data would be more or less the same, since we are already in an RPC-like camp.

+6
source

For POST requests, it is usually preferable to use method 2, although often the resource name will be multiple, so you are actually sending a message:

 http://localhost:8080/services/processusers 

This is for creating new entries.

It looks like you are really using the fact that most RESTful services will use a GET request to (get the record), in which case method 1 is preferred.

Edit: I understand that I did not answer my questions, so let's look at the standards set by Rails . You may or may not agree that this is a valid standard.

0
source

All Articles