![利用Postman发送json请求测试后端响应[2022513],第1张 利用Postman发送json请求测试后端响应[2022513],第1张](/aiimages/%E5%88%A9%E7%94%A8Postman%E5%8F%91%E9%80%81json%E8%AF%B7%E6%B1%82%E6%B5%8B%E8%AF%95%E5%90%8E%E7%AB%AF%E5%93%8D%E5%BA%94%5B2022513%5D.png)
- 1-利用Springinit新建项目
- 2-pom文件导入fastjson包
- 3-测试代码
- 4- postman
- 5-网页打开
com.alibaba
fastjson
2.0.2
3-测试代码
package com.wxlifeassistanttest.Controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.sql.SQLException;
@Controller
public class HelloController {
@RequestMapping(path="/test",method = RequestMethod.GET)
public String login_html(){
return "forward:test.html";
}
@RequestMapping(path = "/test", method = RequestMethod.POST)
@ResponseBody
public JSONObject Hello(@RequestBody(required = false) JSONObject test) throws SQLException {
System.out.println("接收到前端信号");
JSONObject json = new JSONObject();
if(test.get("msg").equals("1")){
json.put("msg","success");
}else {
json.put("msg","fail");
}
return json;
}
}
test.html放在static中
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
</body>
</html>
4- postman
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)