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.

3
  • possible duplicate of RequestMapping in xml -- note that the Spring JIRA ticket referenced is still in the backlog... Commented Aug 4, 2014 at 14:00
  • I wouldn't say its a duplicate because I don't want to use xml. Commented Aug 4, 2014 at 14:04
  • Have you looked at Spring Data REST? Commented Aug 4, 2014 at 17:27

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.