@Relationship between Controller/@RestController/@ResponseBody annotation
1.@Controller
It is used to label the controller layer and is mainly used to process the response in the view. He decides how to call the entity Bean of the Model, how to call business operations such as data addition, deletion, modification and query in the business layer, and how to return the results to the view for rendering.
/** * @Controller Annotation annotation controller layer, which is used to the corresponding page and return the template page (such as html page) */ @Controller public class HelloController { @RequestMapping("/test") public String sayHello(){ return "hello"; } }
If the @ Controller annotation is used separately, after starting the Spring boot project, enter: localhost:8080/test in the address bar, and the following error message will be reported:
2021-07-17 18:09:17.632 ERROR 8968 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "hello": Error resolving template [hello], template might not exist or might not be accessible by any of the configured Template Resolvers
Error parsing template [hello]. The template may not exist or cannot be accessed by any configured template parser. The problem is that the static template page is not used, that is, the static page without hello name under the static resource, that is, the @ Controller annotation is used to respond to the page.
Engineering structure:
hello.html page code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>Hello world</h1> </body> </html>
After creating the static page, start the Spring boot project again and enter: localhost:8080/test in the address bar. The results are as follows:
2.@ResponseBody
Its function is to convert the object returned by the method in the controller to the specified format through the converter, and then write it to the body area of the Response object. It is often used to return data in JSON/XML format.
Note: after using this annotation, the data is directly written to the input stream without view rendering
The @ ResponseBody annotation is usually used with the @ Controller annotation
/** * @Controller Annotation annotation controller layer, which is used to the corresponding page and return the template page (such as html page) */ @Controller //@ResponseBody public class HelloController { @ResponseBody @GetMapping("/test2") public String sayHello2(){ return "hello2"; } }
After adding the @ ResponseBody annotation to the method, start the Springboot project and enter: localhost:8080/test2 in the address bar. The results are as follows:
Note: @ ResponseBody annotation can be written on a class or method. Writing on a class affects all methods in the class, and writing on a method only affects this method.
3.@RestController
It is used to return JSON (JavaScript object notation), XML (eXtensible Markup Language) and other data, but it cannot return HTML (HyperText Markup Language) pages. It is equivalent to the combination of @ ResponseBody and annotation @ Controller.
/** * @RestController Annotation is used to return JSON, XML and other data (it can be understood that the returned data is string or other data) * @RestController=@Controller+@ResponseBody */ @RestController public class HelloController { @RequestMapping("/test") public String sayHello(){ return "hello"; } }
After adding @ RestController annotation to the method, start the Springboot project and enter: localhost:8080/test in the address bar. The results are as follows:
Summary:
@The Controller annotation marks the Controller layer. It is used to respond to the page and return the model page (such as html page)
@RestController annotation is used to return JSON, XML and other data (it can be understood that the returned data is string or other data)
@The Controller annotation marks the Controller layer. It is used to respond to the page and return the model page (such as html page)
@RestController annotation is used to return JSON, XML and other data (it can be understood that the returned data is string or other data)
@RestController=@Controller+@ResponseBody