1

In our web-app we have both @Controller classes and @RestController.

How would you suggest to organize them in our internal project structure project (packages/module) and url mapping wise?

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Johnny
  • 14,397
  • 15
  • 77
  • 118

1 Answers1

1
  1. Concerning Package structure you have two options. layer vs component packaging.

    Layer packaging preserves policy that layer beans are in same packages (e.g. UserController, InvoiceController are in com.example.application.web package and UserService, InvoiceService are in com.example.application.service package). This is most common in the wild.

    Component packaging preserves policy package per feature (e.g. UserController, UserService are in com.example.application.user and InvoiceController, InvoiceService are in com.example.application.invoice).

    I had experience only with Layer packaging so far and gained opinion that Component packaging would solve a lot of problems (especially coupling problems as you can often use package private beans instead of public). So if it would be up to me, I would go with latter approach.

    But your team has to decide which structure to use and stick with it.

    You can read more about pros and cons in this SO thread

  2. Concerning controller naming, it depends if you are doing REST or VIEW templating. Rest APIs would be good to reflect URL resource in Controller name. If you are handling VIEW templating with Spring MVC, it would be good to reflect view names in controller. This naturally creates policy one view/REST resource per controller, which I would follow for sure.
  3. URL structure depends on your domain. If you have a lot of resources, views you want to group them into categories and reflect these categories in URL ("/shopping/invoice", "/profile/user"). I don't know your architecture (SOA vs Monolith), but in SOA architecture may use root level URLs for routing to particular services. So your service may naturally handle some separate domain concerns.
Community
  • 1
  • 1
luboskrnac
  • 23,973
  • 10
  • 81
  • 92