【MapSheep】
[好记性不如烂笔头]

引入Jar包

  1. hutool工具类
    <!--hutool工具类 用于发送异步请求-->
    <dependency>
    	<groupId>cn.hutool</groupId>
    	<artifactId>hutool-all</artifactId>
    	<version>4.3.2</version>
    </dependency>
    

SpringMVC

  1. 接口支持
    //Get请求
    @RequestMapping("/getOne")
    @ResponseBody
    public Staff getOne(Long employ) {
        return staffDao.get(employ);
    }
	
    //Post请求
    @PostMapping("/getOne")
    @ResponseBody
    public Staff getOne(@RequestBody Staff staff) {
      return staffDao.get(staff.getEmpNo());
    }

请求

  1. Get请求
        //定义请求URL
	String url = "http://localhost:8888/staff/getOne?employ=6";
	//一个完整的HTTP请求
	String body = HttpRequest.post(url).timeout(2000)
			.execute()
			.body();
	System.out.println(JSON.toJSON(body));
	//TODO: {"empNo":6,"eName":"赫舍里.索尔图","proVince":"北京市御花园","deptld":"4","groupU":"4","age":"16","sex":"男"}
  1. Post请求
        //定义请求URL
	String url = "http://localhost:8888/staff/getOne";
	//定义参数
	Staff staff = new Staff();
	staff.setEmpNo(6L);
	//一个完整的HTTP请求
	String body = HttpRequest.post(url).timeout(2000)
			.body(JSON.toJSONString(staff))
			.execute()
			.body();
	System.out.println(JSON.toJSON(body));
	//TODO: {"empNo":6,"eName":"赫舍里.索尔图","proVince":"北京市御花园","deptld":"4","groupU":"4","age":"16","sex":"男"}
posted on 2021-06-23 15:48  (Play)  阅读(385)  评论(0编辑  收藏  举报