利用Postman发送json请求测试后端响应[2022513]

利用Postman发送json请求测试后端响应[2022513],第1张

利用Postman发送json请求测试后端响应
  • 1-利用Springinit新建项目
  • 2-pom文件导入fastjson包
  • 3-测试代码
  • 4- postman
  • 5-网页打开

1-利用Springinit新建项目 2-pom文件导入fastjson包
       
            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


5-网页打开

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/918724.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存