fgonzalez fgonzalez - 1 year ago 152
reST (reStructuredText) Question

Restful API, good practices

This question is more about design than a technical problem.
I'm developing a backend application using

Spring Rest Services
It is a small application so in the beginning I just created a controller for all the end points (5 or 6 end points). I have for example an endpoint to request a token i.e
, and another to make a request, based on the token received previously i.e

So now I'm wondering if I should split the controller into two or more controllers, each one with the end points that are related to each other.

Of course in terms of legibility of the code this is useful, but also, from a technical point of view, if the default scope of a spring bean is
, if I have only one controller, that would make only a single instance for the whole application, so let's imagine we have two requests that arrive to the server at the same time, even if each request is running a completely separate thread, and they are requesting different end points, in the end they are accessing the same instance so, one request should wait for the other to finish, we cannot execute on the same instance two different threads at the same time, am I right?

So... in terms of performance or good practices, is it better to avoid big controllers with many end points to have instead many small controllers?

What do you think about it?

Thank you!

Answer Source

we cannot execute on the same instance two different threads at the same time, am I right?

Wrong. This is only the case if the method that is being called has the synchronized access modifier. Otherwise, concurrent calls can occur on the same instance in different threads.

Having multiple controllers has no obvious impact on the performance of the application. It means an extra bean is loaded into memory, which equates to a few extra KB of RAM taken up.

This cost is far outweighed by having code that can be read and understood easily. Remember, you shouldn't write code for yourself. You should write it for the next guy, or as a man much smarter than myself once said..

Write your code as if the next person to read it is an angry psychopath, and he knows where you live.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download