Home >Java >javaTutorial >What is the role of @RestController in SpringBoot http

What is the role of @RestController in SpringBoot http

王林
王林forward
2023-05-20 15:31:06834browse

@RestController

@RestController = @Controller @ResponseBody. The two comrades on the right side of the equal sign briefly introduce a few sentences to understand the meaning of our @RestController:

@Controller will be the current The modified class is injected into the SpringBoot IOC container, so that the class is instantiated when the project where the class is located is run. Of course, it also has a semantic effect, which means that this class acts as a Controller.

@ResponseBody Its function in short refers to the data returned by all API interfaces in this class, regardless of the return of your corresponding method. Map or other Object, it will be returned to the client in the form of a Json string. I tried it. If the returned type is String, it will still be 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 part of the code returns JSON String for Map, and still String for String

When @ After RestController is replaced with @Controller, the return value for /test is as follows:

As you can see from the error report, when @Controller is modified, Spring thinks it will return a View (that is, the C in MVC) but What is returned is a Map.

The above is the detailed content of What is the role of @RestController in SpringBoot http. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete