SpringBoot Controller interface receives data (suitable for Mengxin)

Previously, we discussed how to interpret the love letters sent to us by different little sisters. Of course, those little sisters are easy to understand. As the saying goes, a woman has a needle in her heart. So, brothers! We should continue to work hard on how to tease our little sister. Today's little sister is different from the one in front. They give more things. And different little sisters also wrapped the gifts.
Today, let's talk about the two most common packaging methods: json and form data

1, Receive data json format

Well, no more nonsense, please go to our first little sister today.

The little sister uses json to wrap gifts. As for how to pack it, ladies and gentlemen.. Ask your little sister. I only teach you how to dismantle it. For the little sister who uses json to wrap gifts, she is a little obsessive-compulsive. I can't say that. Anyway, when she prepares gifts for you, she will ask you whether you like them in advance and let you choose. What happens in this way is that you actually know what gift your little sister will give you.
Back to the code. In other words, to accept json data, you need to create an entity class according to the parameters of the data. For example, here we name this entity class Gift. The code is as follows:

public class Gift  {
	private String qingshu;
	private String liwu1;
	private String liwu2;
	public int getQingshu() {
		return this.qingshu;
	}
	public void setQingshu(int qingshu) {
		this.qingshu = qingshu;
	}
	public String getLiwu1() {
		return this.liwu1;
	}
	public void setLiwu1(String liwu1) {
		this.liwu1 = liwu1;
	}
	public String getLiwu2() {
		return this.liwu2;
	}
	public void setLiwu2(String liwu2) {
		this.liwu2 = liwu2;
	}
}

Oh, let's explain here that I use the architecture of springboot+mybatis+xml to show it. As for this aspect, you can refer to other documents or tutorials yourself. Ha, there are still many.
The emphasis here is to create an entity class to accept json data. How to accept it? Look at the following code:

@PostMapping(path = "/jack")
public void demo1(@RequestBody Gift gift) {
    System.out.println(gift.toString());
}

Here we use the annotation * * @ RequestBody * *. He meant to accept the gift wrapped by his little sister. But again, we need to know what we need to receive.
Then if you want to take out part of the gift, such as liwu1 inside. The code only needs to be added as follows:

@PostMapping(path = "/jack")
public void demo1(@RequestBody Gift gift) {
    System.out.println(gift.liwu1.toString());
}

The received result is:
qingshu:ILOVEYOU;liwu1: mobile phone; liwu2: computer

I think it's easy to understand. json is currently the most common way for simple data transmission. The data returned by our back-end code to the front-end is generally in json format.
Well, the first little sister, frank and direct. The introduction has been completed. I don't know if you like it or not. I like it anyway.

2, Form data format data receiving

Next is the second type of little sister. This little sister belongs to Romanticism. She gives gifts. You don't need to know what's inside in advance. Of course you can. You can also choose not to know. In terms of code, we don't need to construct the corresponding entity class if we only receive form data for other functions. Let's look at the gift wrapped by our little sister first.

As for reception, it's not difficult. There are two forms. First, if you are clear about what your little sister wants to give you, I mean if there is a corresponding entity class, as follows:

@PostMapping(path = "/jack")
public void demo2(Gfit gfit) {
    System.out.println(gfit.toString());
}

The reception result is still:
qingshu:ILOVEYOU;liwu1: mobile phone; liwu2: computer

What's more interesting here is that if you thought the little sister gave only one gift. You only received one gift, and that's all right. For example, the following code:

@PostMapping(path = "/jack")
public void demo2(String liwu1) {
    System.out.println(liwu1);
}

The result is:
mobile phone
Isn't that interesting. And more powerful. This little sister can also satisfy your desires. For example, at present, only qingshu+liwu1+liwu2 are good. But you think all these are OK, but what you want most is a photo of your little sister. So you made this request. It's not as follows

@PostMapping(path = "/jack")
public void demo2(Gift gift ,String liwu3 ) {
    System.out.println(gift.toString());
    System.out.println(liwu3);
}

What will happen. If the little sister doesn't agree, it liwu3 is empty. If she agrees. Then you can receive the data. Do you think this little sister is also good.
. . .
The reception of json and form data is introduced here. Receive other data types later. I'll make it up when I'm free. Guys!!! Please press one button three times. Progress together!!! Go, go, go.

Tags: JSON Spring Boot interface

Posted by brij_theinvader on Wed, 04 May 2022 02:31:48 +0300