@ RestController annotations

In Spring, @restController is equivalent to @Controller + @responseBody. So to understand the @RestController annotation you need to understand the @Controller and @responseBody annotations.

@ Controller annotations

Adding the @Controller annotation to a class indicates that the class is a Controller class. But it takes more than the @Controller annotation to make this class a request handler. It needs to be refined to become a handler.

1) Create an instance of the class in the Spring container. There are two ways to create an instance:

<bean class = "test.controller.MyController"/>
Copy the code

The above method is to inject a single bean into the Spring container. When the project is large and the controller class is large, injecting beans into the Spring container in this way is very annoying. There is a second method.

<context:component-scan base-scan="test.controller"/>
Copy the code

This approach scans all classes in the specified package and generates the appropriate beans for injection into the Spring container. Using this approach can certainly greatly improve our development efficiency, but sometimes we don’t want a certain type of class to be injected into the Spring container. At this time, the second way can also be solved.

<context:component-scan base-package="test">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
Copy the code

The code above means scanning the test package for classes that do not have the @service annotation.

2) Injecting the @Controller annotated class into the Spring container is just the first step for the class to become a processor. To make things better, add the @requestMapping annotation to the class.

The @RequestMapping annotation is used to map requests, that is, specify which URL requests the processor can handle. This annotation can be used on both classes and methods. When you use @requestMapping to mark a controller class, the request address of the method is relative to the request address of the class. When the @RequestMapping tag class is not used, the method’s request address is an absolute path. The @requestMapping address can be a URI variable, and the @pathVariable annotation gets the parameters as a method. It can also be a wildcard to filter the requested address. The specific method of use is not the focus of this study.

@Controller
@RequestMapping("/user")
public class UserController{

  @RequestMapping("/users")
  public String users(a) {
      return "users"; }}Copy the code

The url path to the users method is:… / user/users. As you can see above, the users method returns a string value, which is the page the processor will jump to after processing the task. If you want the method to return the result directly instead of jumping to the page, use the @responseBody annotation.

@ ResponseBody annotations

@responseBody indicates that the return value of the method is written directly to the Http Response body in the specified format, rather than being parsed as a jump path. Conversion of the format is done through methods in HttpMessageConverter, and because it is an interface, it is done by its implementing class.

If you want a method to return JSON-formatted data rather than a jump page, you can label @RestController directly on the class rather than @responseBody in each method, simplifying the development process.

Ref: @restController