Atul
Atul

Reputation: 2711

Which is better: returning a Response object or an Object representing the rest resource?

In some books, the rest APIs generally return a Response object which wraps some other objects representing payload, status, etc.

On the other hand many of the APIs that I have seen and written return a POJO (or call it a DTO) as JSON which is the consumed by the client.

This may be opinion based, but I would like to know which is better of the two to use on high scalability environment where some request result in success and others in failure/data not returned.

I would like to know if there is an accepted better practice. This will help me designing some APIs and put things in perspective before my team. But I am ok with this question being closed if 'better of the two' is too much opinion based.

Thanks.

Update: The two rest APIs would look like this. Avoiding code like @Path, @Get, @PathParam, @Produces etc

public Response myCustomerObject(int id){...} 

This returns a Customer object wrapped inside the Response object. Response could be error as well.

And the approach below will return the Customer entity directly:

public Customer myCustomerObject(int id){...} 

Upvotes: 5

Views: 11682

Answers (3)

wero
wero

Reputation: 32980

I would vote for an API which gives you a Response object. This allows you to control the response completely within your code and it is clear what is going into response. And in cases where you want to write a Response which cannot easily be represented by a POJO you are not forced to use unintuitive workarounds.

Returning a Object from a rest handler method which is then transformed into a response is too much framework magic for my taste.

Worst - imho - is the practice to return a String from the rest handler method which is then interpreted as a template reference (e.g. the path to a JSP resource) which is then written to the response. Again way too much magic.

Upvotes: 6

toadzky
toadzky

Reputation: 3846

I prefer returning custom data objects instead if response objects. The whole point of the annotation based frameworks is to abstract away the http aspects from the application logic. Instead of managing response codes and entities, application developers return model objects and throw custom exceptions that can be mapped to http codes in a mapper. It's less control but IMHO it's way easier to rapidly develop an api.

Upvotes: 5

Bassem Reda Zohdy
Bassem Reda Zohdy

Reputation: 12942

The one returning response object contains your data is getting legacy of request/response services like SOAP and HTTP, but REST services build on concept of resources not request/response so I prefer to use objects represents your actual resources without wrapper, how could rest service represents your resources direct on response object for ex. if you are calling resource like car:

http://localhost/car GET for list of cars

http://localhost/car/1 GET for getting car with id 1

how to represent this in response object ?

Upvotes: 1

Related Questions