Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is best practice to package structuring Spring project?

i have spring cloud project and per microservice i think i have many packages. also i have model package witch contains entity-s and custom request response objects .

question 1 : what must be entity classes package name ? entity or model ?

question 2: where must save Searchrequest & Searchresponse and other request response classes ? in model package or i must create other package ?

question 3 : exits some Standard for package structuring ? (pleas give me link )

enter image description here

like image 966
ZURA Tikaradze Avatar asked Jan 27 '23 02:01

ZURA Tikaradze


2 Answers

There is no specific standard structure, It will vary according to your need.

  1. Entity classes should be under domain package, which does not to be appended with the class name. It can be like Book.class, School.class, etc..,
  2. Search request and response is the request and response class, It should come under the controller package. These are all service end points.
  3. check the link

Mostly the SERVICE package will have all the service classes which have business logic and to connect with DAO classes, Controller will have all the rest end points, Model package will contain the pojo classes.

like image 81
Arun Prasat Avatar answered Jan 30 '23 04:01

Arun Prasat


There is not strict rules. You have to use a naming that is self explanatory (not needed to dig into the classes content to understand its meaning). That is right for micro-service, web application, batch and any kind of applications.

I think i have many packages

The main role of packages is to sort things in a human readable way.
For example if you have 2 or 3 classes by package, you can wonder on the relevance of them.
Similarly if you have 50 classes in a package, you can also wonder if you should not split them in subpackages.
Note that the package has also a role in terms of accessibility : indeed the package private access level allows to set/reduce the accessibility of a class or a class member to the classes of the current package. It may also be very useful in terms of design and of isolation.

About your 3 questions :

question 1 : what must be entity classes package name ? entity or model ?

In Java, entity refers generally to JPA entity (overall in a Spring project). While model refers to a more general concept that includes entity but not only since the data model can also be the DTO objects and any data specialization of your model.
If you use a single data objects layer (JPA entities that are also used as JSON representation) , using the model term makes completely sense. If you use several level of abstraction, a package by abstraction level makes more sense.

question 2: where must save Searchrequest & Searchresponse and other request response classes ? in model package or i must create other package ?

If these objects go through every layer : in model, otherwise probably elsewhere.

question 3 : exits some Standard for package structuring ? (please give me link )

3) Not really but as a rule of thumb, it should be self explanatory, very clear and shared by the team of the projects to favor consistency between projects.

like image 36
davidxxx Avatar answered Jan 30 '23 03:01

davidxxx