wonder
wonder

Reputation: 31

Best performance for method void vs non void return type?

There is a web service which is for insert/update. I need to return the response entity, but not always. There is always return value, but user don't need to use always. So that, is it better to write another service with returning the response entity or just one service is enough?

This insert/update service is used in integration project. So it is used for like 500k or more entity, thats why I care the performances.

Upvotes: 0

Views: 686

Answers (1)

Andreas
Andreas

Reputation: 159086

I believe the comments to the question (3 so far) are talking about internal service code in Java, i.e. about the call between a Spring @Controller and a Spring @Service.

But question said this is a web service, which means it's actually asking about an HTTP POST (or PUT) request, where there isn't always anything to send back.

Even if the Spring Controller method is void, an HTTP response is sent back, just with 204 No Content instead of the usual 200 OK + payload. There is no performance difference that you'll ever see, because any time spent formatting/sending a payload with data is microscopic compared to everything else going on.

Note that the appropriate response to an insert is 201 Created.

If you don't want the client to wait, you should probably implement background processing of the request, e.g. by using @Async, so the HTTP request gets an immediate response, even though the server is still working on it. In that case, the response should be 202 Accepted.

Upvotes: 1

Related Questions