RestTemplate请求

参考资料:
https://www.jianshu.com/p/f3d0b4be5524

POST请求

  @Test
    public void test() {
        String url = "http://192.168.1.16:8080/api/inner/coupon/getCouponTljs";
        // 设置请求的 Content-Type 为 application/json
        MultiValueMap<String, String> header = new LinkedMultiValueMap();
        header.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON_VALUE));
// 设置 Accept 向服务器表明客户端可处理的内容类型
        header.put(HttpHeaders.ACCEPT, Arrays.asList(MediaType.APPLICATION_JSON_VALUE));
// 直接将实体 Product 作为请求参数传入,底层利用 Jackson 框架序列化成 JSON 串发送请求
        HttpEntity<CouponTljReq> request = new HttpEntity<>(new CouponTljReq("shopping", "saveMoney", 1, 10));
        ResponseEntity<CouponTljResp[]> exchangeResult = restTemplate.exchange(url, HttpMethod.POST, request, CouponTljResp[].class);
        CouponTljResp[] body = exchangeResult.getBody();
        System.out.println("post_product2: " + exchangeResult);
    }

GET请求

  String url = goodsServerUrl + "/exchange/round/recreation";
        RoundTljResp[] roundTljResps = restTemplate.getForObject(url, RoundTljResp[].class);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章