Java两个项目之间的对接接口

可以用httpclient来进行连接,但是spring都封装在RestTemplate中了,所以直接调用就可以

接口1:

@Autowired
RestTemplate restTemplate;

    @RequestMapping(value = "/interface1", method = RequestMethod.POST)
    public JSONArray merchantDetail() throws IOException {
        JSONObject jsonObject = null;
        List map = restTemplate.postForObject("http://localhost:7070/demo/interface2", jsonObject, List.class);
        JSONArray jsonObject1 = new JSONArray(map);
        ReturnValue rv = ReturnValue.success();
        rv.setData(jsonObject1);
        return jsonObject1;
    }

接口2:

@RequestMapping(value = "/demo/interface2", method = RequestMethod.POST)
    public List interface2() throws IOException {
        List<Demo> demoList = new ArrayList<>();
        Map map = new HashMap();
        List list = new ArrayList();
        for (int i=0; i<3; i++){
            Demo demo = new Demo();
            demo.setId("000"+i);
            demo.setCode("4568971");
            list.add(demo);
        }
        return list;
    }
posted @ 2021-11-12 14:39  张三丰学Java  阅读(896)  评论(0编辑  收藏  举报