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; } }以上部分仅供参考,有问题大家一起谈论,勿喷。