In implementing layered API architecture with Spring MVC Boot, how to connect to various endpoints

by berlinbrown2   Last Updated January 11, 2018 23:05 PM

If you want to implement a layered API design for example, you may have one API layer that represents the application layer. And I assume that the application layer is represented by an endpoint and uri. Let's say we implement a Spring MVC Boot based controller, within that controller we may make a httpclient rest call or use SpringRest Template invocation to another layer, say the infrastructure api layer.

My question centers around the chaining and calling different apis, is spring rest template or httpclient directly used? Or Something else more sophisticated? I ask , because it seems cumbersome replicating a lot of domain objects. Can you share model/domain objects between different layers?

Answers 1

Different layers do not correspond to different end-points in a network. These are really separate concerns with distinct purposes.

Layers vs. End-Points

Layers are about structuring your code and keeping dependencies in check, e.g. by not allowing the domain model to know about your views. They separate your application into horizontal slices, such as Presentation, Data Access, Application Logic, Domain Logic.

Every application will have multiple layers, regardless of whether it is distributed. Consider:

  • A standalone desktop application with no networking capabilities - will have multiple layers, but only one end-point.

  • A client/server system. Each client will have multiple layers, and so will the server.

  • A peer-to-peer system. The same application may run on many end-points, but each will have all of its layers.

  • A system of microservices. Each service will have multiple layers.

Each layer may be developed as a separate module, such as a library. But in the end they all have to become part of the same application. Now in addition to the layers, you might slice your application vertically, i.e. by function, and run each of them in a different application on a different end-point.

With that out of the way, let's turn to your specific questions:

How to the end-points communicate?

It depends, but usually, your controller will receive a request (e.g. REST), extract the relevant data and pass it to the application layer.

The application layer may make a call to another end-point. However, it will not use anything technology-specific like a RestClient, but go through an interface. The class that implements the interface will then use whatever it has to.

Can you share objects between the layers?

Yes, in fact you have to. But there will/should be restrictions on which layer may reference which objects - that's the whole point of the layers. There are different approaches to organizing layers (e.g. traditional 3-tier, onion architecture) that define which layer may have access to the objects of which other layer.

Can you share classes between end-points

You obviously can't share objects - at best you can serialize, transmit and re-instantiate them. But the class definitions may absolutely be shared between end-points. There are many ways in which this may happen:

  • Multiple peer-to-peer endpoints may be just different instances of the same application. Here, all classes are shared.

  • Client/Server may or may not share some layers completely (e.g. domain layer) or partially (serialization module from the infrastructure layer).

  • Microservices really shouldn't share much code.

Also note that you'll want to keep the communication between end-points to a minimum (as far as possible). This is also part of the reason why you generally don't have layers on different end-points. As a request typically has to go through all layers, it would be a huge performance hit.

Can multiple controllers share objects?

Multiple controllers of the same application can obviously have access to all parts of the application layer they need.

January 19, 2018 20:13 PM

Related Questions

Handling domain errors in the API

Updated May 02, 2018 11:05 AM

Primitive vs Class to represent simple domain object?

Updated October 30, 2017 07:05 AM