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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章