9

I have a REST call that accepts a JSON object, lets say, a person. After I create this object (validated and saved to the database), I need to return the newly created JSON Object.

I think the standard practice is to return 201 Accepted instead of returning the object immediately. But my application needs the newly created object immediately.

I have a controller methods that take a POST call, calls a service class, which in turn calls a DAO that uses Hibernate to create the object. Once it's saved to the database, I am calling another controller method that takes the ID of the person and returns the Object.

My question, is this the better approach? i.e., calling another Controller method to get the newly created object. Or the POST call itself should return the Object.

The main question is: Calling another method takes a round trip and I guess it's an overkill. (Service->DAO->Hibernate->Database). Instead I think I should get the Object from the database immediately after it's saved in the same call (from the method that handled POST).

What is the architecture standard here?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

3 Answers3

23

Try using ResponseEntity which returns HTTP status along with the object you need.

Sample code is (this was my code where I am returning Customer object, change it as per your needs) :

// imports (for your reference)
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

// spring controller method
@RequestMapping(value = "getcust/{custid}", method = RequestMethod.GET, produces={"application/json"})
public ResponseEntity<Customer> getToken(@PathVariable("custid") final String custid, HttpServletRequest request) {

    customer = service.getCustById(custid);

    return new ResponseEntity<Customer>(customer, HttpStatus.OK);
}

Read this documentation to know more. Some sample code has been provided there.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
  • 2
    This is for GET. But what if you want to return the customer Object with POST. That is create and then return that immediately. Do you call the above GET method to return or you fetch the JSON object immediately after you save it, in the same POST call method? – Kevin Rave Apr 22 '13 at 15:17
  • This is a sample code, for your understanding. I just wanted to explain you how you can return an object along with http status. If you are using hibernate, you can get persisted object which you can return immediately along with Http (201) status. – Jeevan Patil Apr 22 '13 at 16:56
5

From the HTTP specification for POST:

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

What you will return in the response body will depend on how strictly you interpret an entity which describes the status of the request and refers to the new resource - and many implementations simply return a representation of the newly created entity itself. The most important thing is to set the Location header in the response to be the URI of the newly created resource, so that clients may immediately fetch it if they so choose.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • The question is on the approach to return the created entity. 1.) By calling the another controller GET method or 2.) Returning from the same POST method that created the entity. – Kevin Rave Apr 21 '13 at 13:49
  • Did you read my answer? Or are you asking how to specifically return an entity from a POST in Spring? – Perception Apr 21 '13 at 13:58
  • Yes, that answers the first part. "What should be returned". But the other part of the question is "what is the standard/better way to return the object itself in the response body". Not in the location header. I understand and agree that I should return `201 Created` and then location/URI of that resource in the header. But our requirement is to return the actual JSON object in the body itself. Can you please let me know? – Kevin Rave Apr 22 '13 at 00:04
  • 1
    `and many implementations simply return a representation of the newly created entity itself` - aka, either just return the entity directly from your post method, or wrap it in a result object and return it. – Perception Apr 22 '13 at 08:52
0

You can return the entity object just after persisting it using @ResponseBody, after @ResponseStatus, but it is not standard, so your client has to be aware of this customization, otherwise if your client depends on standard APIs you have to stick to the standard by returning void.

user3481644
  • 398
  • 2
  • 12
Bassem Reda Zohdy
  • 12,662
  • 3
  • 33
  • 39