What is the purpose of @ RestController in SpringBoot http
This article mainly introduces the relevant knowledge of the role of @ RestController in SpringBoot http, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading what the role of @ RestController in SpringBoot http is. Let's take a look at it.
@ RestController
@ RestController = @ Controller + @ ResponseBody is composed of two comrades on the right side of the equal sign to briefly introduce two sentences to understand the meaning of our @ RestController:
Controller injects the currently decorated class into the SpringBoot IOC container so that the class is instantiated during the run from the project in which the class is located. Of course, it also has the function of semantics, that is, it means that this class acts as a Controller.
In a nutshell, the function of @ ResponseBody refers to the data returned by all API APIs in this class. Regardless of whether your corresponding method returns Map or other Object, it will be returned to the client in the form of a Json string. I have tried it. If String is returned, it is still String.
@ RestController@RequestMapping ("test") public class SampleController {
@ GetMapping public Map testGet () {
Return new HashMap () {{
Put ("name", "springboot")
}}
}
@ GetMapping (path = "str")
Public String testGetStr () {return "OK";}}
This code returns JSON String for Map and still String for String
When @ RestController is replaced with @ Controller, the return value for / test is as follows:
As you can see from the error report, when @ Controller modifies, Spring thinks it will return a View (that is, the C in MVC), but what is returned is a Map.
This is the end of the article on "what is the role of @ RestController in SpringBoot http". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the role of @ RestController in SpringBoot http". If you want to learn more, you are welcome to follow the industry information channel.