Jeecg代码搬砖圣地第六篇(接口交互)

1.服务端

  1.1 在系统管理---->接口权限------>接口用户管理里录入客户端发送过来的用户名test和密码123456

  1.2 启动redis

2.客户端代码

public static String getToken(String userName,String password){
        String url = "http://localhost:8080/jeecg/rest/tokens?username="+userName+"&password="+password;
        String token=httpRequest(url, "POST", null);
        return token;
    }
    
    
    //获取黑名单列表
    public static JSONObject getBlackList(String token){
        String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
        JSONObject resp=httpRequest(url, "GET", null,token);
        return resp;
    }
    
    //创建黑名单
    public static JSONObject createBlackList(String token,String json){
        String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
        JSONObject resp=httpRequest(url, "POST", json,token);
        return resp;
    }
    
    
    //更新黑名单
    public static JSONObject updateBlackList(String token,String json){
        String url = "http://localhost:8080/jeecg/rest/tsBlackListController";
        JSONObject resp=httpRequest(url, "PUT", json,token);
        return resp;
    }
    
    
    //删除黑名单
    public static JSONObject deleteBlackList(String token,String id){
        String url = "http://localhost:8080/jeecg/rest/tsBlackListController/"+id;
        JSONObject resp=httpRequest(url, "DELETE", null,token);
        return resp;
    }
    
    //查询黑名单
    public static JSONObject getBlackList(String token,String id){
        String url = "http://localhost:8080/jeecg/rest/tsBlackListController/"+id;
        JSONObject resp=httpRequest(url, "GET", null,token);
        return resp;
    }
    
    
    public static void main(String[] args) {
        String token = getToken("test","123456");
        System.out.println(" token : "+ token);
//        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhZG1pbiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNTExODU5NjM2fQ.Emfe8VZKA_L33jaW8ZUtVFVDEin83Np_d3gKlPIZryE";
        
        //添加黑名单
//        JSONObject jsonObject=new JSONObject();
//        jsonObject.put("ip","192.168.1.2");
//        System.out.println("======添加黑名单======="+createBlackList(token,jsonObject.toJSONString()));
        //更新黑名单
//        JSONObject jsonObject=new JSONObject();
//        jsonObject.put("id","402881ee6001da57016001dc13110001");
//        jsonObject.put("ip","192.168.0.111");
//        System.out.println("======更新黑名单======="+updateBlackList(token,jsonObject.toJSONString()));
        //删除黑名单
//        System.out.println("======删除黑名单======="+deleteBlackList(token,"402881ee6001da57016001dc13110001"));
        //查询黑名单
//        System.out.println("======查询黑名单======="+getBlackList(token,"402881ee6001e873016001f369f40008"));
        //获取黑名单列表
        System.out.println("======获取黑名单列表======="+getBlackList(token));
    }
public static String httpRequest(String requestUrl, String requestMethod, String outputStr) {
        String res = "";
        StringBuffer buffer = new StringBuffer();
        HttpURLConnection httpUrlConn = null;
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            URL url = new URL(requestUrl);
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            httpUrlConn.setRequestProperty("Accept", "text/plain");
             httpUrlConn.setRequestProperty("Content-Type", "application/json");
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod(requestMethod);
            if ("GET".equalsIgnoreCase(requestMethod))
                httpUrlConn.connect();

            // 当有数据需要提交时
            if (null != outputStr) {
                OutputStream outputStream = httpUrlConn.getOutputStream();
                // 注意编码格式,防止中文乱码
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            res = buffer.toString();
            log.debug(res);
        } catch (ConnectException ce) {
            LogUtil.info("Weixin server connection timed out.");
        } catch (Exception e) {
            e.printStackTrace();
            org.jeecgframework.core.util.LogUtil.info("https request error:{}" + e.getMessage());
        } finally {
            try {
                httpUrlConn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
                org.jeecgframework.core.util.LogUtil.info("http close error:{}" + e.getMessage());
            }
        }
        return res;
    }

  

原文地址:https://www.cnblogs.com/xujiating/p/12097144.html