Good morning, in my project I'm using three-tier architecture, I have one package for controller, and one for service, and another one for data model and repositories (I'm using spring data).
So I need to create some classes just for the web services in the controllers package.
My question is, it is a best practice to create those classes in the controllers package or in the model package or the model package can just have the types to be persisted?
Thank you in advance.
After many years working with multilayer architectures, the best practice is to group the classes in a way that is meaningful to you and your developers. See the following example:
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── mycompany
│ │ │ └── myproject
│ │ │ └── mymodule
│ │ │ ├── Application.java
│ │ │ ├── data
│ │ │ │ ├── bo
│ │ │ │ │ ├── Account.java
│ │ │ │ │ ├── Customer.java
│ │ │ │ │ ├── User.java
│ │ │ │ └── repository
│ │ │ │ ├── AccountRepository.java
│ │ │ │ ├── CustomerRepository.java
│ │ │ │ └── UserRepository.java
│ │ │ ├── exception
│ │ │ │ ├── DuplicatedRecordException.java
│ │ │ │ ├── NoResultException.java
│ │ │ │ └── UnknownErrorException.java
│ │ │ ├── web
│ │ │ │ └── controller
│ │ │ │ ├── AccountController.java
│ │ │ │ ├── CustomerController.java
│ │ │ │ └── UserController.java
│ │ │ └── service
│ │ │ ├── AccountService.java
│ │ │ ├── CustomerService.java
│ │ │ └── UserService.java
│ │ └── resources
│ │ ├── application.yml
│ │ ├── bootstrap.yml
│ │ ├── logback-spring.xml
│ │ └── messages
│ │ ├── message_es.properties
│ │ └── message.properties
The previous example group classes in packages by:
BO (Business Object) Entities that represents data
Exception Your custom exception classes
Web Controller If you are going to build rest all your controllers should be here.
Service Your services classes if needed.
Resources All your application resources