In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the difference between Controller and RestController? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
In springboot, Controller and RestController are the two most commonly used annotations for using controllers, but do you know the difference between them? Here is the difference between the two.
1. What Controller and RestController have in common
Are used to indicate whether a class of Spring can receive HTTP requests.
2. Differences between Controller and RestController
Controller: identifies that a Spring class is a Spring MVC controller processor, @ RestController:@RestController is a combination of @ Controller and @ ResponseBody, and the two annotations are combined. The methods in the @ Controller class can jump to jsp, ftl, html, and other template pages directly by returning String. You can also return entity objects by annotating the method with @ ResponseBody. All methods in the @ RestController class can only return entity objects such as String, Object, Json, etc., and cannot jump to the template page.
If the method in @ RestController wants to jump to the page, it is encapsulated with ModelAndView, as follows:
@ RestControllerpublic class UserController {@ RequestMapping (value = "/ index", method = RequestMethod.GET) public String toIndex () {ModelAndView mv = new ModelAndView ("index"); return mv;}}
Examples are as follows:
Controller @ ResponseBody public class MyController {} @ RestController public class MyRestController {}
@ Controller annotation source code:
Package org.springframework.stereotype;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.stereotype.Component;@Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Componentpublic @ interface Controller {String value () default "";}
@ RestController annotation source code:
Package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ResponseBody;@Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Controller@ResponseBodypublic @ interface RestController {String value () default "" } this is the answer to the question about what is the difference between Controller and RestController. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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: 271
*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.