In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the difference between @ Controller and @ RestController in springboot, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
The difference between @ Controller and @ RestController and the difference between @ Controller and @ RestController
When the control layer uses the annotation @ Controller in springboot development, the method annotated with @ GetMapping (@ PostMapping or @ RequestMapping) returns a view, while the value returned using @ RestController corresponds to json data, and @ Controller+@ResponseBody is equivalent to @ RestController.
Application of @ Controller
Configure it in the application.properties configuration file first
Spring.mvc.view.prefix=classpath:/templates/spring.mvc.view.suffix=.html
Then the code for the CustomerController class in the control layer is
Controllerpublic class CustomerController {@ Resource CustomerServiceI customerServiceI; @ GetMapping ("/") public String index () {return "redirect:/list";} @ GetMapping ("/ list") public String list (Model model) {List users = customerServiceI.getUserList (); model.addAttribute ("users", users); return "list";}}
Enter the localhost:8080/list access page in the browser after starting the program, which is the list.html under the templates folder.
Application of @ RestController
The code for the control layer CustomerController class is
@ RestControllerpublic class CustomerController {@ Resource CustomerServiceI customerServiceI; @ GetMapping ("/") public String index () {return "redirect:/list";} @ GetMapping ("/ list") public List list (Model model) {List users = customerServiceI.getUserList (); model.addAttribute ("users", users); return users;}}
After starting the program, enter localhost:8080/list in the browser and the access effect is as follows
The difference between @ Controller and @ RestController
The difference between the two is actually a very simple question, but it may fall into a pit for beginners.
The @ RestController annotation is equivalent to the combination of @ ResponseBody + @ Controller.
1. If the annotated Controller uses @ RestController
Then the method in Controller cannot return the jsp page, or html, the configured view parser InternalResourceViewResolver does not work, and the content returned is the content in Return.
The code is shown below:
The result is shown in the figure:
two。 If you need to return to the specified page (jsp/html)
You need to use @ Controller with the view parser InternalResourceViewResolver.
The code is shown below:
The result is shown in the figure:
If you need to return JSON,XML or custom mediaType content to the page, you need to annotate the corresponding method with @ ResponseBody.
The code is shown below:
The result is shown in the figure:
The above is all the content of the article "what's the difference between @ Controller and @ RestController in springboot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.