1

Im creating an API using Java and Spring. My question is, is there a standard way to organize the API routes into one file?

For example when creating an API using Express.JS there is one file, called the router, where all of the routes are declared and set up.

With Spring's annotation-based MVC framework it seems like the routes are scattered through various controllers. So if someone who didn't write the API needed to make changes to it they would be left searching through files to find the specific route.

Is there a standard practice or pattern that would create a central router? Im thinking about just creating a router class however I would then have to create instances of MANY classes in that router. It doesn't seem very clean.

RachelD
  • 4,072
  • 9
  • 40
  • 68
  • possible duplicate of [RequestMapping in xml](http://stackoverflow.com/questions/4481373/requestmapping-in-xml) -- note that the [Spring JIRA ticket](https://jira.spring.io/browse/SPR-5757) referenced is still in the backlog... – ach Aug 04 '14 at 14:00
  • I wouldn't say its a duplicate because I don't want to use xml. – RachelD Aug 04 '14 at 14:04
  • Have you looked at Spring Data REST? – Neil McGuigan Aug 04 '14 at 17:27

1 Answers1

2

XML configuration used to be the only way to do it, but if I remember correctly, the usual usage was to have one method per controller.

There's a fairly nice implementation of what you're looking for in the third-party springmvc-router project, which will let you configure your routes something like:

GET     /user/?                 userController.listAll
GET     /user/{<[0-9]+>id}      userController.showUser
DELETE  /user/{<[0-9]+>id}      userController.deleteUser
POST    /user/add/?             userController.createUser
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67