1. 程式人生 > >Spring boot框架請求資料庫的url地址

Spring boot框架請求資料庫的url地址

url地址及請求引數是已知的:

//依據使用者openId判斷使用者以前是否已登入過,如未登入,則新增到資料庫
logger.info("{}---{}", accessToken, openId);
JSONObject userInfo = weChatAuthService.getUserInfo(accessToken, openId);
//呼叫後臺介面
String checkurl="V1/OrgManager/getUsers?";
AuthInfo authInfo = new AuthInfo("", "", "", "");
String authInfoJson = JSON.toJSONString
(authInfo); String queryCase = "filter=&&name="+openId.trim()+"&&authInfo="+authInfoJson+"&&pageable=false"+"&&pageSize=2"+"&&pageNum=0"+"&&sortDesc=name:desc"+"&&orgId=e9292051-2ede-11e7-8c78-c85b767a1aee"; JSONObject jsonResult= DubboService.invokeService
(logger,checkurl,queryCase); if(jsonResult.get("isOk")==true) { Map<String,JSONArray> nickname = new HashMap<String,JSONArray>(); Map<String,JSONArray> headimgurl = new HashMap<String,JSONArray>(); model.addAttribute("nickname",nickname); model.addAttribute("headimgurl"
,headimgurl); } else { String createurl = " V1/UserManager/createUser?"; String UserCase = "filter=&&name="+openId.trim()+"&&authInfo="+authInfoJson+"&&pageable=false"+"&&pageSize=2"+"&&pageNum=0"+"&&sortDesc=name:desc"+"&&orgId=e9292051-2ede-11e7-8c78-c85b767a1aee"; JSONObject jsonUser= DubboService.invokeService(logger,createurl,UserCase); }
以上就部分程式碼:這裡DubboService是暴露給你的介面;checkurl就是介面文件裡面的url;UserCase是查詢條件,這是訪問資料庫的介面認證。如果訪問成功將部分引數傳給前端,如果訪問不成功將得到引數寫入資料庫
以下是DubboService方法:
package com.hitrobotgroup.hiiri.nobody.security.service;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2017/8/10.
 */
public class DubboService {
    private static HttpClient client = HttpClientBuilder.create().build();
    public static String serverIp="http://192.168.1.119:8099/";
    public static JSONObject invokeService(Logger logger, String uri, String queryCase) {
        HttpPost request = new HttpPost(serverIp + uri);
        logger.info(queryCase);
        StringEntity reqEntity = null;
        try {
            reqEntity = new StringEntity(queryCase);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        reqEntity.setContentType("application/x-www-form-urlencoded");
        request.setEntity(reqEntity);
        JSONObject jsonResult=null;
        try {
            HttpResponse response = client.execute(request);
            String strResult = EntityUtils.toString(response.getEntity());
            jsonResult = JSONObject.parseObject(strResult);
            logger.info("result:" + jsonResult);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonResult;
    }
}
以上部分僅供參考,有問題大家一起談論,勿噴。