I have a project with several spring boot modules. Our data layer is not the simpler, more typical situation of CRUD and finders for model objects, and there is more configuration and knowledge of metadata that is needed for services that interact with our data. If I have a module that consolidates the configuration that is important to several of the other modules, and if I registered these instances as spring beans, what would happen if I returned them from a rest endpoint to consumers? Let us say, for example, that one of these objects is a custom MongoRepository
implementation. Would the returned instance be functional? If two modules called the endpoint to request the instance, would the instances be functional when they were deserialized at the consumer modules? Is there a difference if the consumers are on different JVMs than the service that hosts the rest endpoints?
I am unsure if the answer is as straightforward as it might seem when we consider what happens when a model object is sent by a rest controller and received by a consumer. A repository includes an established connection to the database that may or may not be SSL. With things like authentication and authorization, it seems like this approach would not be feasible, but I thought I would ask the question to make sure that I really understand this. I am weighing this approach against creating this module as a library (jar) that I can include in the modules that need this configuration. I suspect that I will only be able to do this with the jar/library method, though.
You shouldn't expose infrastructure beans like repositories, services, datasources etc. as resources through your (Rest) endpoints. That will only lead to (weird) issues.
If you would, for instance, expose your MongoRepository
and it would be serialized (through java Serialization
) why should it work on my computer? I don't have the connection to MongoDB, I might not even have it nor I might not even care about that. Imagine the same with a JpaRepository
connecting to your Enterprise Grade database servers like Oracle or DB2. I definitely won't have that, nor all the schemas and data.
So as a rule of thumb don't expose those services through (rest) endpoints.
Your (rest) endpoints should only expose models and not infrastructure (or application) related beans.