In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the difference between @ Controller and @ RestController annotations in Java". In daily operation, I believe many people have doubts about the difference between @ Controller and @ RestController annotations in Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the difference between @ Controller and @ RestController annotations in Java". Next, please follow the editor to study!
Differences between @ Controller and @ RestController annotations:
The @ RestController annotation is equivalent to the combination of @ Controller + @ ResponseBody.
1. @ Controller comments
@ Controller is an annotation provided by the Spring framework.
The class identified by @ Controller, which represents the controller class (control layer / presentation layer). Here, each method in the control layer can call the class identified by @ Service (business logic layer), and the method in the class identified by @ Service can continue to call the interface implementation class identified by @ Resposity (Dao layer / persistence layer).
[MVC design mode: M (model) refers to the model, V (view) refers to the view layer, and C (controller) commands the control layer. M represents the model generally refers to service and DAO;view represents the view generally refers to the page eg:jsp,html ftl and so on; C represents the controller, such as controller in springMVC or action in struts2. In fact, MVC we normally understand should be VMC:JSP/HTML page data-> controller-> data logic processing and persistence. But it's officially MVC, so let's just remember it according to MVC. ]
The @ Controller tag cannot really say that it is the controller of SpringMVC on a class, because Spring doesn't know it yet, so you need to give the controller to Spring to manage at this time. Manage in the following ways:
Configure the following scanners in the applicationContext.xml file
The above scanner can scan all components under the "com.baidu" package and its subpackages with @ Component, @ Controller, @ Service, @ Resposity, and so on, and add them to the IOC container, managed by the IOC container.
@ Controller is used to mark a class, and the class marked with it is a SpringMVC Controller class, and the distribution processor scans the method of the class that uses the annotation and detects whether the method uses the @ RequestMapping annotation.
@ Controller simply defines a controller class, and the method annotated with @ RequestMapping is the processor that handles the request.
@ Controllerpublic class HospitalController {/ / injects Service service object @ Autowired private HospitalService hospitalService; @ RequestMapping (method = RequestMethod.POST, value = "/ findAllHospital") @ ResponseBody public Map findAllHospital (final HttpServletRequest request, @ RequestBody Map parmMap, HttpServletResponse response) {Map map=parmMap; / / @ RequestBody annotated the reason, the system will automatically change request data into parmMap. . Return map;}}
Define a controller class with @ Controller
Use @ RequestMapping to give the path of the external access method, or trigger path, trigger condition.
Mark the methods in the Controller class with @ ResponseBody. Return the result of return as a JSON object. Without this annotation, this method can only return the path to which you want to jump, that is, the html/JSP page of the jump. With this note, you can return only JSON data instead of jumping to the page)
2. @ RestController comment
@ RestController is also an annotation provided by the Spring framework. (added after Spring4.0)
The @ RestController annotation is equivalent to the combination of @ Controller + @ ResponseBody.
The method return value in the Controller class defaults to the json object, which is equivalent to adding @ ResponseBody to the method in @ Controller. If the method returns a value and needs to jump, then the return type of the method must be View or ModelAndView.
@ RestControllerpublic class HospitalController {/ / injects Service service object @ Autowired private HospitalService hospitalService; @ RequestMapping (method = RequestMethod.POST, value = "/ findAllHospital") / / method does not need @ ResponseBody annotation, because the class uses @ RestController annotation public Map findAllHospital (final HttpServletRequest request, @ RequestBody Map parmMap, HttpServletResponse response) {Map map=parmMap; / / @ RequestBody annotation reason, the system will automatically turn request data into parmMap. . Return map;}}
At this point, the study of "the difference between @ Controller and @ RestController annotations in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.