Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 852
user811433 Avatar asked Oct 09 '13 23:10

user811433


People also ask

What is the recommended project structure for spring boot REST projects?

There is no specific layout or code structure for Spring Boot Projects. However, there are some best practices followed by developers that will help us too. You can divide your project into layers like service layer, entity layer, repository layer,, etc. You can also divide the project into modules.

Can we make REST API using Spring MVC?

Starting with Spring 3.0, Spring introduced first-class support for creating REST APIs. And Spring's REST implementation has continued to evolve through Spring 3.1, 3.2, and now 4.0.

What is @RestController in spring boot?

Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.


1 Answers

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

like image 78
digao_mb Avatar answered Sep 28 '22 11:09

digao_mb