TIL: Spring Boot, pt3

Still working my way through the tutorial. Lots of new concepts for me today, so I didn’t push through as much as I had hoped. But at least I learned a lot! Lots of juicy stuff like introducing me to singletons as well as basic dependency injection. Woo~

Just like the past few days, this is going to be more note dumping.

More Controller Stuff (mapping to method call)

  • We need to create a base class so that we can create some dummy data until we have access to the database (covering later)
  • We start by making a new class (Topic in this example)
  • Create class variables and then create getters and setters for each of them
    • Intellij can do this automatically for you! Go to Code -> Generate -> Getters and setters, and then select all of the variables for which they should be created.
  • Spring is going to automatically convert the object that we are returning into the correct JSON format for us. There’s nothing we need to do
  • In our example with sample data, we can do the following to initialize a bunch of dummy data to be returned
1
2
3
4
5
6
7
8
@RequestMapping("/topics")
public List<Topic> getAllTopics() {
   return Arrays.asList(
           new Topic("spring", "Spring Framework", "Spring Framework Description"),
           new Topic("java", "Core Java", "Core Java Description"),
           new Topic("javascript", "JavaScript", "JavaScript Description")
   );
}
  • The returned json keys will be equal to the class variables used in the main data class (Topic in this case)
    • Values will be what we passed in

Business Services

  • Just a normal Java class
  • In spring, business services are typically singletons
  • Class is marked using the @Service annotation in Spring
  • You can then make reference to this class by using dependency injection by using the @Autowired annotation.

Notes on Singletons

  • Notes coming from one of Derek’s videos.
  • Singletons are essentially a class that can only ever be instantiated once
  • If you try to create a second object using that class, it will error out as only one can be created
  • This is useful if you have a resource that should be shared amongst things and you don’t want multiple instances being created
  • Essentially it will check to see if the resource already exists. If it does, the resource is returned. If it doesn’t, it will be created, stored, and returned.

Lazy Instantiation

  • The way of only creating the instance if it is needed. If it’s not needed, it will never be created.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Singleton {
    private static Singleton firstInstance = null;
    private Singleton() { }

    public static Singleton getInstance() {
        if (firstInstance == null) {
            firstInstance = new Singleton();
        }
    }
}

Variables in Path

  • In order to support resources with variables in them, there’s two additional things that you need to do
  • The first is when defining the resource path. You’ll need to place the variable inside of {} brackets with the variable name inside to tell spring that it’s a variable
  • The next comes an annotation when defining the input to the method. You need to provide a @PathVariable inside of the input field to tell Spring how to map the variable in the path to the variables in the method.
  • If your variables match (path and method), then you only need the @PathVariable annotation.
  • If the variables don’t match, you’ll need to do @PathVariable("name")
1
2
3
4
   @RequestMapping("/topics/{id}")
   public Topic getTopic(@PathVariable("id") String id) {
       return topicService.getTopic(id);
   }

Handling Post Requests

  • Extra annotation changes needed in order to change the specific method supported on a path
    • Note that you can actually break out the request logic based on method type (meaning having multiple methods for the same route)
  • Need to define supported method as well as specify route as a value parameter:
1
   @RequestMapping(value = "/topics", method = RequestMethod.POST)
  • In order for us to get the request body, we need another annotation called @RequestBody which will tell spring to grab the request body at that path:
1
2
3
4
@RequestMapping(value = "/topics", method = RequestMethod.POST)
   public void addTopic(@RequestBody Topic topic) {
       topicService.addTopic(topic);
   }
  • Evidently you don’t even need to do the method type with RequestMapping as there’s actually an annotation for each of the types (if you’re only supporting one)
    • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping all exist and will specify the method for you!

💚