How to use the @RestController annotation in Spring Boot

@RestController annotation in Spring Boot

In Spring Boot, we often need to write RESTful web services to facilitate communication between clients and servers. In order to simplify the development of RESTful Web services, Spring Boot provides the @RestController annotation, which allows us to write RESTful Web services more conveniently.

In this article, we will introduce what the @RestController annotation does, how it works, and how to use it in a Spring Boot application.

insert image description here

The role of @RestController annotation

The @RestController annotation is an annotation used to declare RESTful Web services in Spring Boot. Its functions are as follows:

  1. Declare the class as a RESTful Web service: The @RestController annotation tells Spring Boot that this class is a RESTful Web service that will be used to handle HTTP requests sent by clients.

  2. Automatic conversion to JSON or XML: @RestController annotation can automatically convert the return value to JSON or XML format, which is convenient for client-side parsing and processing.

  3. Simplify code: The @RestController annotation can greatly simplify the development of RESTful Web services, reducing the amount of code and redundant operations.

The principle of @RestController annotation

The @RestController annotation is a combined annotation provided by Spring Boot, which includes @Controller and @ResponseBody annotations. Among them, the @Controller annotation is used to declare a class as a controller, and the @ResponseBody annotation is used to tell Spring Boot that the return value needs to be converted to JSON or XML format.

The following is the source code of the @RestController annotation:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    
    

    @AliasFor(annotation = Controller.class)
    String value() default "";

}

As can be seen from the above code, the @RestController annotation contains the @Controller and @ResponseBody annotations, and their functions are:

  1. @Controller annotation: Declare a class as a controller that can handle HTTP requests sent by clients.

  2. @ResponseBody annotation: Tell Spring Boot that the return value needs to be converted to JSON or XML format.

Therefore, using the @RestController annotation can make it easier for us to write RESTful Web services, reducing the amount of code and redundant operations.

How to use @RestController annotation

In Spring Boot, using the @RestController annotation is as simple as adding it to a class definition. Here is an example:

@RestController
public class MyController {
    
    

    @GetMapping("/hello")
    public String sayHello() {
    
    
        return "Hello, World!";
    }

}

In the above example, we use the @RestController annotation to declare a class MyController, which contains a GET request processing method sayHello(), which returns the string "Hello, World!".

In this example, we have used the @GetMapping annotation to declare a GET request handler whose path is /hello. When the client sends a GET request to the /hello path, Spring Boot will automatically call the sayHello() method, convert the return value into JSON or XML format, and return it to the client.

In addition, the @RestController annotation also supports other HTTP request processing methods, such as:

@RestController
public class MyController {
    
    

    @GetMapping("/hello")
    public String sayHello() {
    
    
        return "Hello, World!";
    }

    @PostMapping("/users")
    public void createUser(@RequestBody User user) {
    
    
        // 创建用户
    }

    @PutMapping("/users/{id}")
    public void updateUser(@PathVariable Long id, @RequestBody User user) {
    
    
        // 更新用户
    }

    @DeleteMapping("/users/{id}")
    public void deleteUser(@PathVariable Long id) {
    
    
        // 删除用户
    }

}

In the above example, we have used @PostMapping, @PutMapping and @DeleteMapping annotations to declare POST, PUT and DELETE request processing methods, which are used to create, update and delete users respectively.

in conclusion

The @RestController annotation is an annotation used to declare RESTful Web services in Spring Boot, which allows us to write RESTful Web services more easily, reducing the amount of code and redundant operations. Using the @RestController annotation allows us to focus more on the implementation of business logic without paying too much attention to the processing of requests and responses. In practical applications, we can use annotations such as @GetMapping, @PostMapping, @PutMapping and @DeleteMapping to declare HTTP request processing methods to facilitate communication between the client and the server.

Guess you like

Origin blog.csdn.net/2301_77835649/article/details/131411351
Recommended