不同springCloud项目互相调用接口

有A,B两个springCloud项目,B项目的提供restful接口供A项目调用,B项目不在A的注册中心,其实跟普通的http接口调用方法一样

代码

A项目

1.调用接口类

public static String defaultConnection(String method, String path, int timeout, int readTimeout, String data)
            throws Exception {
        URL url=null;
        URLConnection con=null;
        HttpURLConnection urlCon=null;
        String strResponse=null;
        StringBuilder sb=new StringBuilder();
        int intResponseCode = HttpURLConnection.HTTP_OK;
        OutputStream out=null;
        InputStream input=null;

        //第1步,建立连接
        try {
            url=new URL(path);
            //向某个特定协议对象返回表现http资源连接的引用
            con=url.openConnection();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http连接失败:"+e.getMessage());
        }

        //第2步,验证连接的类型,必须是HttpURLConnection
        if(!(con instanceof HttpURLConnection)){
            System.out.println("http连接失败,连接类型错误");
        }

        //第3步,发送报文
        try {
            //表明程序必须把名称/值对输出到服务器程序资源
            con.setConnectTimeout(timeout == 0 ? 1000 : timeout);
            con.setReadTimeout(readTimeout == 0 ? 1000 : readTimeout);
            con.setRequestProperty("Content-Type","application/json");


            con.setDoOutput(true);
            con.setDoInput(true);
            //表明只能返回有用的信息
            con.setUseCaches(false);
            urlCon=(HttpURLConnection)con;
            //设置HTTP请求方法
            urlCon.setRequestMethod(method);
            //获得输出流对象
            out=urlCon.getOutputStream();
            DataOutputStream dos=new DataOutputStream(out);
            dos.write(data.getBytes("utf-8"));
            dos.flush();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http发送失败:"+e.getMessage());
        }finally{
            out.close();
        }

        //第4步,校验返回状态
        try {
            intResponseCode=urlCon.getResponseCode();
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("http返回状态失败:"+e.getMessage());
        }
        if(intResponseCode!=HttpURLConnection.HTTP_OK){  //如果不为(HTTP_OK)200,说明服务器返回错误
            System.out.println("request "+ urlCon.getURL() +" fail. response: code="+intResponseCode +
                    ", message="+urlCon.getResponseMessage());
        }


        //第5步,接收报文
        try {
            input=urlCon.getInputStream();
            //将字节流转换为字符流
            BufferedReader br=new BufferedReader(new InputStreamReader(input));
            while((strResponse=br.readLine())!=null){
                sb.append(strResponse);
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("http接收错误:"+e.getMessage());
        }finally{
            input.close();
            //关闭和服务器的连接
            urlCon.disconnect();
        }
        return sb.toString();
    }

2. 调用方法

public void testtoken(){

        String uuid = "{\"accessToken\":\"123456\"}";
        System.out.println("uuid=="+uuid);
        String responseEntity = "";
        try {
            responseEntity = defaultConnection("POST", "http://localhost:50666/receiveToken", 10000, 10000, uuid);
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("responseEntity=="+responseEntity);
    }

B项目

@RequestMapping(value = "/receiveToken", method = RequestMethod.POST)
    public void receiveToken(@RequestBody UserParamDto user,HttpServletResponse response) throws Exception {
        System.out.println(user.getAccessToken());
        PrintWriter out = null;
        try {
            out = response.getWriter();
            System.out.println(out);
            out.write("{\"loginName\":\"11223344\"}");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

结果

A

B

 

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值