欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RestTemplate

程序员文章站 2022-04-28 18:30:46
...

Spring Boot中http请求的发送与接收

1.get请求

  1. get发送请求格式:http://localhost:8080/test?name=Tom&age=13

    // 方式一
    String uri = "http://localhost:8080/test?name={name}&age={age}";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "Tom");
    params.put("age", "13");
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class, params); // 方式一返回内容:<200,result,{Content-Type=[text/plain;charset=UTF-8], Content-Length=[6], Date=[Tue, 24 Jul 2018 11:59:04 GMT]}>
    // 可以通过一下方式获取result的不同字段的值
    result.getStatusCode() // 200
    result.getBody() // result
    result.getHeaders() // {Content-Type=[text/plain;charset=UTF-8], Content-Length=[6], Date=[Tue, 24 Jul 2018 11:59:04 GMT]}
    
    // 方式二
    String uri = "http://localhost:8080/test?name={name}&age={age}";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "Tom");
    params.put("age", "13");
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class, params); // 方式二返回内容:result

    接收请求代码

    // 方式一
    @RequestMapping(value="/test", method=RequestMethod.GET)
    @ResponseBody
    public String testGet(String name, String age) {
    String result = "result";
    logger.info("name:{} age:{}", name, age);
    return result;
    }
    
    // 方式二
    @RequestMapping(value="/test", method=RequestMethod.GET)
    @ResponseBody
    public String testGet(HttpServletRequest request) {
    String result = "result";
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    logger.info("name:{} age:{}", name, age);
    return result;
    }
    
    // 方式三
    @RequestMapping(value="/test", method=RequestMethod.GET)
    @ResponseBody
    public String testGet(Person person) { // 建立一个带有name和age属性的Persion类
    String result = "result";
    String name = person.getName();
    String age = person.getAge();
    logger.info("name:{} age:{}", name, age);
    return result;
    }
    
    // 方式四
    @RequestMapping(value="/test", method=RequestMethod.GET)
    @ResponseBody
    public String testGet(@RequestParam("name") String name, @RequestParam("age") String age) {
    String result = "result";
    logger.info("name:{} age:{}", name, age);
    return result;
    }
  2. get发送请求格式http://localhost:8080/test/Tom

    // 发送方式就是上面的两种,本质都一样,举一个例子说明
    String uri = "http://localhost:8080/test/{name}";
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "Tom");
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class, params);

    接收请求代码

    @RequestMapping(value="/test/{name}", method=RequestMethod.GET)
    @ResponseBody
    public String testGet(@PathVariable("name") String name) {
    String result = "result";
    logger.info("name:{}", name);
    return result;
    }

2.post请求

  1. post发送请求格式:http://localhost:8080/testPost/Tom?age=13&height=170

    String uri = "http://localhost:8080/testPost/{name}?age={age}&height={height}";
    RestTemplate restTemplate = new RestTemplate();
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("name", "Tom");
    params.put("age", "13");
    params.put("height", "170");
    String request = "this is request body";
    ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class, params);

    接收请求代码

    @RequestMapping(value = "/testPost/{name}", method = RequestMethod.POST)
    @ResponseBody
    public String testPost(@RequestBody (required=false) String requestBody, @PathVariable("name") String name, @RequestParam("age") String age,String height) {
    String result = "result";
    logger.info("requestBody:{}", requestBody);
    logger.info("name:{}", name);
    logger.info("age:{}", age);
    return result;
    }
  2. post发送请求格式:http://localhost:8080/testPost/Tom (参数在请求体中)

    String uri = "http://localhost:8080/testPost";
    RestTemplate restTemplate = new RestTemplate();
    String request = "this is request body";
    ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class);

    接收请求代码

    @RequestMapping(value = "/testPost", method = RequestMethod.POST)
    @ResponseBody
    public String testPost(@RequestBody (required=false) String requestBody) {
    String result = "result";
    logger.info("requestBody:{}", requestBody);
    return result;
    }

相关标签: RestTemplate