26

I am new to writing a spring rest based ws. I created a project with the following structure.

Java Resources
  - src/test/java
  - src/main/java
     - com/sample/rest
       - controller  (for the request mappings)
       - domain (for POJOs)
       - service (for business logic)
       - utility (for utility methods)
       - dao (for database calls)

I started adding POJOs in the domain package, but my problem is that I have 2 kinds of POJOs in my application. One type which corresponds to my application table structure. Another type which corresponds to a third party result structure.

I am not sure how I can differentiate these 2 POJO types under my domain package.

burnttoast11
  • 1,164
  • 16
  • 33
user811433
  • 3,999
  • 13
  • 53
  • 76
  • This is a little confusing since, according to Spring terminology, they're all [POJOs](https://spring.io/understanding/POJO). – Brent Bradburn Mar 13 '18 at 21:30

2 Answers2

41

most projects look like what you described. Inside domain package would have a user package where it would have all user related pojos. On dao, service would exist the same sub packages too.

But an organization that I think it's best is to split the packages is this way:

-com.company.project
    - users
         UserService
         UserDAO
         User
         Role
    - cart
         Cart
         CartService
         CartDAO
         ShopItem

And so it goes. I saw it for the first time on talk from a guy from Spring Source. I'll try to find the video.

Anyway, I'm working on a project with this strategy for some months, and until now it seems more organized than the traditional way.

If a package, for example users, become too crowded, you can always create subpackages to organize inside it. But for most packages it will be 1 or 2 domain classes, one DAO and one Service. So there's no need for more packages.

Update: I think this is the video: http://www.youtube.com/watch?v=tEm0USdF-70

digao_mb
  • 1,294
  • 16
  • 23
  • 2
    This scales much better than the common dao/controller/service/entity approach. As a side note: Everytime I introduce this to developers they are shocked (and thus are not willing to give it a try) how many packages eclipse shows in his flat package view (default). Just switch to the hierarchical representation and you are fine. – atamanroman Oct 10 '13 at 08:24
  • 3
    Keep in mind that if you are using Spring and would want to instantiate only specific types of classes (say only model without DAOs) you would not be able to use components scan efficiently. – Alex Fedulov Dec 03 '14 at 17:42
  • What is the usecase for instantiating only specific types of classes? (serious question :) ) – Kevin Wittek Jan 26 '15 at 10:39
  • @digao_mb How should this be altered (if any) for a project that uses Spring MVC but also Spring-Data? – java123999 Mar 02 '16 at 00:19
  • We use the same model with Spring Data too. – digao_mb Mar 02 '16 at 11:40
10

Lets come think from the module/library point of view.

Good to have separated core business logic library outside the application , keep it separate with the test library, and rest library to embrace the core business logic library by utilize the functionality inside the core business logic.

Module : MyAppLogic.jar
  -> com.company.user
       -> class UserBean : Pojo
       -> class UserDao : insert( String userName , String userEmail ) ;
       -> class UserService : insert( UserBean userBean ) ;
  -> com.company.cart
       -> class CartBean : Pojo
       -> class CartDao : insert( int cartUserId , int cartItemId ) ;
       -> class CartService : insert( CartBean cartBean ) ;

Module : MyAppRest.jar
  -> com.company.rest.domain
       -> class User : @XmlRootElement
       -> class Cart : @XmlRootElement
  -> com.company.rest.model
       -> interface UserServiceIntf : insert( User user ) ;
       -> class UserServiceImpl : private UserService userService ;
       -> interface CartServiceIntf : insert( Cart cart ) ;
       -> class CartServiceImpl : private CartService cartService ;
  -> com.company.rest.service
       -> class UserRestService : @Path("/users")
       -> class CartRestService : @Path("/carts")
Benny Iskandar
  • 121
  • 2
  • 6