How do you find your way around a Spring codebase?

Viewed 107

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?

2 Answers

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.

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.

Related