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);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值