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

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

WBOY
Release: 2023-05-12 17:40:06
forward
1311 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 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>
Copy after login

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!

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