SpringBoot Section 2: Use of Controller (Controller)

As we all know, the controller is only used to receive the client's request.

Annotations for handling controller and url mapping:

@Controller  handles http requests;

@RestController  This is a new annotation added after Spring 4. It is recommended to use this annotation. The above @Controller is not used, because it also needs @ResponseBody to cooperate with @Controller to return json;

@RequestMapping   configures url mapping;

Access is successful:

Access is successful:

In order to be safe and in line with business scenarios, the post/get method still needs to be added. The post method cannot be accessed in the browser. Postman can be used to send requests:

Annotations for handling url parameters:

@PathVariable get data in url

@RequestParam gets the value of the request parameter

@GetMapping composition annotation

ps: This annotation can be used to receive parameters regardless of the post/get method.

 

Access is successful:

Other properties of @RequestParam:

@GetMapping or @PostMapping, simplified annotation:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340890&siteId=291194637