2

We are developing a new REST Api for our Spring MVC legacy enterprise web application (before this it was communicationg with Adobe Flex frontends).

As a general rule we stated that we were creating a @RestController for every model we have in our application. We have also a BaseController for handling the CRUD operation on every model.

The fact is that the backend of our application is very large and monolithic, counting at least 2-3 thousands model classes, so creating a Controller for each of these its starting to look unrealistic.

On top of this we usa Swagger for API Documentation, so it also seems that for every controller we add, Swagger takes a little time at server startup to map the corresponsing endpoints. I fear this will escalate in very long server startup times (we use Tomcat).

Is it an antipattern to map a Controller for every model class?

Thanks

  • 2
    Look for microservices. Split the applications to different services, deploy independently and largely, easy to modify and enhance. – Arun May 15 '18 at 13:41
  • Even REST API needs encapsulation. If it was not a web API. If it was a regular API in any other language, you would not expose all your entities. You need encapsulation in order to ensure the consistency and the integrity of the data. You provide consumers with an interface which is largely more concise and small than the sum of all the components that compose the whole API. – Laiv Jun 14 '18 at 6:26
1

I don't think mapping a controller to a model would be considered an antipattern. If you didn't map a controller to each model, how else do you currently plan on interacting with that model?

It sounds like the core issue is that there are a lot of model classes and mapping each to a controller will take substantial effort. You mention that the rule was to create a controller for every model, but does each model really need to be interacted with directly or can model classes be combined into a broader 'resource'? For example, if you have a person and a phone number model, do you need to expose the person and phone number separately? Or can they be 'merged' into a single resource to expose? Without knowing what the model classes represent, I would suggest looking at them and determining if there are any higher level resources you would like to expose. If models A, B, and C are components of a 'resource' R, then don't expose them independently but through a controller for the resource R.

If each model is independent and needs to be able to be operated on singly, then I believe each model will need its own controller. If you are looking to speed this process up the only thing I could think of is if the majority of controllers only perform CRUD to automate the generation of those classes, or since you mention you are using Spring, searching for tools within that framework to do some heavy lifting for you (Spring Data Rest project for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.