Home > Java > javaTutorial > body text

What is the role of @RestController in SpringBoot http

王林
Release: 2023-05-20 15:31:06
forward
823 people have browsed it

@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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template