Technology Sharing

Spring MVC Getting Started 2

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Use of Postman

Following the last issue, we raised a question: the use of Postman
You can click the link to download https://www.postman.com/downloads/
insert image description here
After installation, you will be prompted to upgrade the version, just click dissmiss.

To send data, the specific steps are as follows:

insert image description here
There is also a more specific diagram, you can take a look at it first, and the request parameters in it will be explained later.insert image description here

But please note that you must start the IDEA project before sending it, otherwise there will be no results.
I'll send you the code from last time. At this time, I have already started the project in idea, so the result is normal, but if you don't start it, it will definitely be abnormal.

insert image description here

The second key point of learning Spring MVC - request

Accessing different paths means sending different requests. When sending a request, some parameters may be included, so learning Spring requests is mainly about learning how to pass parameters to the backend and how the backend receives them.
To pass parameters, we mainly use browsers and Postman to simulate.

Passing a single parameter

Let me first talk about our basic error information:
404: The resource cannot be found, most likely due to an error in the server or the URL is incorrect.
400: Type Mismatch
500: Missing parameters

  • The steps for passing parameters are as follows: We first pass parameters using the URL (you can use Postman to pass parameters using the URL, but we will use the URL first). As shown below, the parameters after the question mark are the parameters we pass. Remember to make sure the names are consistent. If they are inconsistent, a 400 status code will be reported.

insert image description here

Passing multiple parameters

insert image description here

When there are multiple parameters, the front-end and back-end match the parameters based on the parameter names. At this time, since the parameter names and formal parameters we pass are consistent, the position of the parameters does not affect the result of the back-end obtaining the parameters, so it is not wrong to swap the positions of name and id.
insert image description here
In fact, it can also have a similar effect to overloading
insert image description here

Passing Arrays

At this point, since arrays are different from ordinary parameters, we can use Postman to pass parameters.

insert image description here
The KEY in Postman must be consistent with the parameter we pass, otherwise it will not match, which is equivalent to not being able to assign a value to this parameter, so it defaults to null.
insert image description here

But is there any way we can pass the parameter name even if it is different from the KEY we passed? Of course there is a way: we just need to use the @RequestParam annotation
In some special cases, the parameter key passed by the front end may be different from the key received by our back end. For example, the front end passes an array2 to the back end, and the back end uses the array field to receive it. In this case, the parameter cannot be received.
In this case, we can use @RequestParam to rename the parameter values ​​of the frontend and backend.
insert image description here

Passing Objects

Some people may be curious about how to pass objects. In fact, we just need to pass the attributes one by one. No matter what the order is, our KEY is always consistent with the formal parameter, so there is no need to worry about the order.
insert image description here
The following is the code for Person, which is simpler than calling. If you are interested, you can copy it directly and try it out.

public class Person {
    private int id;
    private String name;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", password='" + password + ''' +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
Transfer Collection

The @RequestParam annotation needs to be used.
Similar to arrays, the same request parameter name can be multiple, and you need to use @RequestParam to bind the parameter relationship. By default, multiple values ​​with the same parameter name in the request are encapsulated into an array. If you want to encapsulate it into a collection, use @RequestParam to bind the parameter relationship.
insert image description here
If this annotation is not used, 500 will be reported:
insert image description here
Since the above method of passing objects is relatively complicated, in the next issue we will introduce another method of passing objects, JSON data representation.

The answer to the legacy question: Does @RequestMapping support post or get?

If you have tried all the examples above, you will be able to verify this problem yourself.
The verification steps are as follows
insert image description here

insert image description here
insert image description here
After switching, we found that it can support both get and post.