3

I'm started a position where the backend is in Spring and I'm having trouble. In Django, Flask, Rails, Express.js, etc, there are route files where you can find the declarations of the different URLs the server has responses configured for, and find your way around from there. Spring doesn't seem to have that....there's just files everywhere with annotations that transform the various classes into endpoints.

How do you find your way around or form some sort of mental conceptualization of the codebase's structure?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • Well you can declare constant of end points in single file – bananas Jun 10 '19 at 04:28
  • 2
    When a Spring Boot application is started, the startup completes with the message `Started [application name] in [startup time] seconds ...`. Just before this message, there are messages like `Mapped "..." onto ...`, which show which URLs have been mapped to which controller methods. Collectively, these log messages serve the same purpose of having a centralized routing file mapping URLs to controllers. One can use an IDE to get to the controller method for a URL and then to other components from there on. – manish Jun 10 '19 at 04:38
  • You have several options. 1) Ideally, you have a naming convention for packages and/or classnames to have the word "controller", 2) the startup log is a great place to look (`debug=true` can help), 3) you can "grep" for "@...Mapping` annotations in your source code. ALSO: look here: https://stackoverflow.com/questions/3686196/how-to-show-all-available-routes-in-spring or here: https://stackoverflow.com/questions/32525699/listing-all-deployed-rest-endpoints-spring-boot-jersey?rq=1 – paulsm4 Jun 10 '19 at 04:51

2 Answers2

3

Here are some general-purpose tips:

  • Java IDEs are great, use them extensively. They can help in many cases. All of them support spring boot applications. So you can open, say, pom.xml file and it will load the project.

  • All Rest Controllers are usually annotated with @RestController annotation

  • If you think the @RestController is too generic and you need something much more fine-grained in your project, consider using Spring Feature called "Stereo Type annotations". In short, you can define your own annotation (like @MyOwnVeryCustomProjectController) that will be itself marked as @RestController so that Spring boot will treat the classes annotated with that custom annotation just like Rest Controllers (you can also set up any parameter with reasonable defaults with this method).

  • If you use Java Configurations as opposed to a purely declarative approach with annotations and component scanning, then you might check whether there are @Configuration classes that aggregate bean definitions for all controllers.

  • Use Spring Actuator in Spring Boot services. In short, it exposes some HTTP endpoints, to any spring-boot process. Among other (fairly useful) endpoints, there is a mappings endpoint, it displays a list of all @RequestMapping paths in Runtime.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
1

Try to find a class marked with the annotation @RestController. This class will serve as a controller to the path in @RequestMapping("/student") with multiple URL/ endpoints marked by any of the one annotations:

@RequestMapping("delete")
@PostMapping("/create")
@GetMapping("/list")
@DeleteMapping("{id}")
@PutMapping("{id}")

Likewise, there can be multiple controllers each, in turn, having multiple endpoints.

Hope this helps.