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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM