springBoot遠程調用restFul接口(RestTemplate)

1、需求說明

有兩個springBoot項目:

項目A將數據讀到HashMap內存中並提供restFul接口供其他程序調用獲取map數據;

項目B則負責先讀取A中的map數據,再做其他邏輯操作;

下面則只介紹項目B的具體實現;

2、測試代碼

使用springBoot自帶的RestTemplate,來遠程調用restFul接口並返回json數據;

(1)restFul接口存在的測試數據

(2)代碼測試

import com.gl.wnmodel.util.MapJsonUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class test {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("id","123456");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<MultiValueMap<String, String>>(map,headers);
        System.out.println("遠程調用傳遞參數:" + formEntity);
        String result = restTemplate.postForEntity("http://127.0.0.1:8099/getMap", formEntity, String.class).getBody();
        System.out.println("遠程調用返回結果:" + result);
    }

}

(3)返回結果

遠程調用傳遞參數:<{id=[123456]},{Content-Type=[application/x-www-form-urlencoded]}>
...
遠程調用返回結果:{"18838517357":"20200407","16678924563":"20200414","18838517356":"20200406"}

3、封裝成方法並返回map

import com.gl.wnmodel.util.MapJsonUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class GetRemoteData {
    public static final GetRemoteData instance = new GetRemoteData();
    public Map<String,Object> getRemoteMap(String url){
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        //map.add("id","123456");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<MultiValueMap<String, String>>(map,headers);
        String result = restTemplate.postForEntity(url, formEntity, String.class).getBody();
        //將json映射爲Map
        Map<String, Object> mapData = MapJsonUtil.jsonToMap(result);
        System.out.println("遠程調用返回結果大小:" + mapData.size());
        return mapData;
    }
}
  public static Map<String,Object> jsonToMap(String jsonStr){
        try {
            Map<String,Object> map2= JSON.parseObject(jsonStr, HashMap.class);
            return map2;
        }catch (Exception ex){
            throw new RuntimeException("json轉map出錯",ex);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章