Simple piece of information leads to great clarity!
Greetings!
The above image just shows a small subset of the Spring framework features used in Runtime.
Most of the time, it ends up generating something as shown in the screenshot above recommending to create a controller with the '@Controller' annotation and return a view.
I've been trying to wrap my head around the Spring framework for over a year now and it's one of the most interesting things I've discovered. Spring framework is an application framework for the Java platform and has many many features!
Spring Framework Runtime |
The above image just shows a small subset of the Spring framework features used in Runtime.
As is the norm now, the way I learn anything is to type into ChatGPT, 'Teach me ...' first before referring to more 'authoritative' sources so I did the same with the Spring Framework.
Snippet of ChatGPT response for 'teach me spring' |
```Java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
// Adding data to the model
model.addAttribute("message", "Welcome to Spring MVC!");
// Returning the view name
return "home"; // This resolves to 'home.html' or 'home.jsp' based on your view resolver configuration
}
}
```
This all makes sense with the MVC architecture. Then, eventually, we all learn about Spring Boot which helps us take off faster since it's an opinionated version of Spring with good defaults for new projects and when trying to learn Spring Boot off the guides [1], we see the following.
```Java
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
```
I'll be honest, it had bothered me for a while that there's a difference, i.e. '@Controller' and '@RestController' but I just took it as it is and went on for the rest of the respective tutorials.
This difference really clicked into place for me while taking the course by Ken Kousen [2] when he explicitly talks about the difference.
1. @Controller
This is the traditional way in which the entire HTML gets rendered on the server side and is sent to the user.
This typically works with a view resolver to return a view template like Thymeleaf, JSP, etc.
2. @RestController
This is the REST API (newer way) of doing things where the controller returns a JSON object that gets rendered by the browser or a front-end framework like React / Angular.
Used for building REST APIs. Directly returns the response body (e.g., JSON or XML) instead of resolving to a view. No need for a view resolver or template engine.
References:
[1] https://spring.io/guides/gs/spring-boot
[2] https://www.oreilly.com/library/view/spring-and-spring/0636920620938/
Comments
Post a Comment