how to define package structure for a Spring REST MVC application?
Asked Answered
D

2

27

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.

Disvalue answered 9/10, 2013 at 23:13 Comment(1)
This is a little confusing since, according to Spring terminology, they're all POJOs.Rattlehead
S
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

Stieglitz answered 10/10, 2013 at 0:32 Comment(5)
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.Homebody
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.Arioso
What is the usecase for instantiating only specific types of classes? (serious question :) )Intarsia
@Stieglitz How should this be altered (if any) for a project that uses Spring MVC but also Spring-Data?Woods
We use the same model with Spring Data too.Stieglitz
A
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")
Amy answered 21/3, 2016 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.