Postman發(fā)送post和get請(qǐng)求json數(shù)據(jù),并用SpringBoot接受
一. idea中的操作
1. 在controller類中加入如下代碼用于舉例
TestContoller.java
package com.example.demo.controller;
import com.example.demo.dto.TestDto;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestContoller {
/**
* 測(cè)試
*
* @return Success
*/
@RequestMapping("/test")
public Object test() {
return "Success!";
}
/**
* 用戶
*
* @param testDto
* @return id password
*/
@RequestMapping("/User")
public Object user(@RequestBody TestDto testDto) {
return "賬戶名:" + testDto.getId() + " 密碼:" + testDto.getPassword();
}
}
這里有兩個(gè)映射,一個(gè)是"/test",用于測(cè)試程序有沒(méi)有成功,一個(gè)是"/User",為用戶,存放用戶的賬戶名和密碼
2. 在dto中導(dǎo)入數(shù)據(jù)以封裝數(shù)據(jù)
TestDto.java
package com.example.demo.dto;
import lombok.Data;
/**
* 封裝參數(shù)
*/
@Data
public class TestDto {
/**
* 用戶名
*/
private String id;
/**
* 密碼
*/
private String password;
}
@Data在導(dǎo)入lombok插件和依賴后可以直接使用,可以起到簡(jiǎn)化代碼的作用
在TestController.java中我們可以看到**@RequserBody**,是用于接受前端傳給后端的json字符串中的數(shù)據(jù)(請(qǐng)求體中的數(shù)據(jù))
二. Postman部署
1.到Postman官網(wǎng)下載并安裝
軟件安裝全部默認(rèn)即可
2.發(fā)送請(qǐng)求
2.1 新建一個(gè)請(qǐng)求
2.2 選擇發(fā)送什么請(qǐng)求
2.3 填寫url
2.4 填寫key和value
在輸入框中填寫文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-407461.html
{
"id":"張三",
"password":"123456"
}
點(diǎn)擊發(fā)送
出現(xiàn)如下代表成功了
Get請(qǐng)求就已經(jīng)發(fā)送并且SpringBoot來(lái)接受json數(shù)據(jù)了,那么Post請(qǐng)求也是一樣的操作文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-407461.html
到了這里,關(guān)于Postman發(fā)送post和get請(qǐng)求json數(shù)據(jù),并用SpringBoot接受的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!