How to organize the project structure of a REST API service? #210
-
My current project structure looks something like this:
I wish to use Spring Modulith and group code based on modules of my business domain. I have found some examples in this discussion. All of them have a different structure, none of which follow the guidelines fully. So I wonder what would be a recommended approach, let's say I get name these three modules: users, polls and posts. Would the correct / recommended structure look like this?
And what if I for example, have multiple controllers and services inside a module? Should I do subgrouping like service and controller, or is that rudimentary? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Before we start, please note that it does not make sense to discuss module structure with only a single module present. Spring Modulith primarily helps to organize code into multiple, functional modules. What kind of technical organization you use within those modules is actually a bit out of scope as it is, essentially, an implementation detail of the module and thus doesn't affect the maintainability of the overall arrangement. Basically, you have two options: Single-package approachSimply stick to the
Choose a technical organization scheme to further structure the content of the moduleIn your case, that would look something like this:
This arrangement doesn't make any difference from a Spring Modulith perspective, as everything in In Onion Architecture, MiscellaneousNote, that packages are not necessarily the best means to assign roles to certain types. Their primary purpose is to provide means of encapsulation, which the simple approach uses to its maximum, as we can solely rely on the compiler to enforce the visibility. Alternatively to packages, you might want to check out jMolecules. That gives you a vocabulary (Onion, Hexagonal Architecture) in the form of annotations and interfaces so that you can assign those architectural roles to types and also get dependency verification via the corresponding ArchUnit rules. |
Beta Was this translation helpful? Give feedback.
-
Maybe use classical project structure.
This modular monolithic structure can quickly locate target modules while making it easy to understand layer isolation. It also has strong scalability support for future splits. My native language is not English, so please forgive me for the wording problem. We can communicate if anything is unclear. |
Beta Was this translation helpful? Give feedback.
Before we start, please note that it does not make sense to discuss module structure with only a single module present. Spring Modulith primarily helps to organize code into multiple, functional modules. What kind of technical organization you use within those modules is actually a bit out of scope as it is, essentially, an implementation detail of the module and thus doesn't affect the maintainability of the overall arrangement.
Basically, you have two options:
Single-package approach
Simply stick to the
user
package. Types that need to be accessed by other modules, need to be declaredpublic
(+
). Everything else stays package private (o
).