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

user811433 picture user811433 · Oct 10, 2013 · Viewed 18.7k times · Source

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.

Answer

digao_mb picture digao_mb · Oct 10, 2013

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