2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Following the last issue, we raised a question: the use of Postman
You can click the link to download https://www.postman.com/downloads/
After installation, you will be prompted to upgrade the version, just click dissmiss.
To send data, the specific steps are as follows:
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.
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.
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.
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
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.
In fact, it can also have a similar effect to overloading
At this point, since arrays are different from ordinary parameters, we can use Postman to pass parameters.
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.
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.
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.
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 + ''' +
'}';
}
}
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.
If this annotation is not used, 500 will be reported:
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.
If you have tried all the examples above, you will be able to verify this problem yourself.
The verification steps are as follows
After switching, we found that it can support both get and post.