“This is the second day of my participation in the August More Text Challenge.

Start with Spring Boot 2.x Quick Start

Modify based on the above code

Using @RestController makes it easy to write REST services, and Spring uses JSON as input and output by default.

1. Write RestController

To write a Rest Controller, just follow these points:

Always tag @RestController instead of @Controller, @Component

With @restController instead of @Controller, each method automatically becomes an API method. Input and output are fine as long as they can be serialized or deserialized to JSON by Jackson.

Write ApiController as follows:

@RestController
public class ApiController
{
    @GetMapping("/users")
    public List<String> users(@RequestParam(value = "user_name") String userName )
    {
        return List.of("Apple"."Orange"."Banana", userName);
    }

    @GetMapping("/user/{user_name}")
    public String user(@PathVariable("user_name") String userName)
    {
        returnuserName; }}Copy the code

@getMapping indicates a GET request, and the value “/ Users “in parentheses indicates the request address

@requestParam is used to get query parameters

@pathvariable Is used to obtain path parameters

If a method parameter needs to be passed to HttpServletRequest, HttpServletResponse, or HttpSession, simply add that type of parameter and Spring MVC will automatically pass it by type

The tests are as follows:

192.168.123.100:8080 / users? user_name=langya

The request parameter user_name is passed in

192.168.123.100:8080 / user/langya

The path is passed in as the langya parameter

2. Request an address group

Grouping urls with one Controller for each group is a good way to avoid duplicate URL mappings. Such as:

@RestController
@RequestMapping("/api")
public class ApiController{}Copy the code

The above API request path becomes a prefix (API) + path, such as/API/usESrs

3. Request and transfer

Common request types:

  • GET: Requests to GET a specific resource from the server.
  • POST: Creates a new resource on the server.
  • PUT: Updates the resource on the server (the client provides the updated entire resource).
  • DELETE: Deletes a specific resource from the server.
  • PATCH: Updates resources on the server (the client provides the changed properties, which can be regarded as partial updates)

POST

  • ReuqestBody primarily handles request parameters in json string format, requiring that the header be specified by the partycontent-type:application/json
  • Requestbodies typically require the caller to use a POST request (other types can be used as well)
  • After the RequestBody receives the data, it automatically binds it to a Java object. The system will useHttpMessageConverterOr customHttpMessageConverterConvert the JSON string in the body of the request into a Java object.

Define the incoming parameter UserParam

The Lombok dependency library is used here — to simplify the code, you don’t have to write a bunch of SET and GET property functions

package com.langyastudio.springboot.bean.dto;

import lombok.Data;

/** * user passes the argument */
@Data
public class UserParam
{
    private String userName;
    private String nickName;
    private String telephone;
}
Copy the code

Defines the interface

@RestController
@RequestMapping("/api")
public class ApiController
{
    @PostMapping("/user/add")
    public UserParam addUser(@RequestBody UserParam userParam)
    {
        return userParam;
    }

    @PostMapping("/user/addex")
    public Map<String, Object> addUserEx(@RequestBody Map<String, Object> params)
    {
        returnparams; }}Copy the code

@RequestBody can either receive incoming parameters using the bean entity class (recommended) or a Map (less readable and maintainable)

The tests are as follows:

192.168.123.100:8080 / API/user/add

Note when using Postman tests, use Body -> RAW -> JSON

A request method can have only one @requestBody, but can have multiple @RequestParam and @PathVariable

Full code: github.com/langyastudi…