Home  >  Article  >  Java  >  How to use @RestController annotation to implement http request in SpringBoot

How to use @RestController annotation to implement http request in SpringBoot

WBOY
WBOYforward
2023-05-12 17:40:061138browse

@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(){{
      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

How to use @RestController annotation to implement http request in SpringBoot

How to use @RestController annotation to implement http request in SpringBoot

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

How to use @RestController annotation to implement http request in SpringBoot

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!

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