@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 your corresponding The method returns Map or other Object, which will be returned to the client in the form of a Json string. I tried it. If the returned type is String, it is still String.
@RestController @RequestMapping("test") public class SampleController { @GetMapping public Map testGet() { return new HashMap<string>(){{ put("name", "springboot"); }}; } @GetMapping(path = "str") public String testGetStr() { return "OK"; } }</string>
This part of the 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:The above is the detailed content of How to use @RestController annotation to implement http request in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!