功能描述:客户端与服务端,用户数据互通,当任何一端修改信息,另一端随之修改。
直接上代码:
客户端调用服务端:
服务端地址:http://localhost:9999(服务地址) /commApi/addUser(接口地址)
//添加用户时,同步单点登录用户信息
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("loginName", sysUser.getLoginName());// 登录名
paramMap.put("passWord", sysUser.getPassWord()); // 密码
// http://localhost:9999/sso (服务地址) /commApi/addUser(接口地址)
String url = "http://localhost:9999/sso/commApi/addUser";
try {
HttpClientUtil.httpPostRequest(url,paramMap);
} catch (Exception e) {
e.printStackTrace();
}
主要使用 http协议,利用post传值。
HttpClientUtil是一个工具类,具体代码放在最后了。
有兴趣的小伙伴可以去搜一下 HttpClient 学习一下。
----------------------------------------------------------------------------------------------
服务端 调用 客户端:
public void userSynchronization (SsoUser ssoUser){
String url = 客户端地址 + "/synchronApi/synchron"(客户端接口);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("loginName", ssoUser.getLoginName());// 登录名
paramMap.put("passWord", ssoUser.getPassWord()); // 密码
String jsonString = JSON.toJSONString(paramMap);
try {
String result = HttpRequest.post(url)
.body(jsonString)
.execute().body();
LOGGER.debug("resLog-result:{}", result);
} catch (Exception e) {
e.printStackTrace();
}
}
这里采用的是json传输的,用了 hutool的方法。
具体的用法和详解可以去hutool的官网看看文档,一定比我说的好
需要在pom.xml文件上,加上如下:引入jar包
//导入hutool的包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.14</version>
</dependency>
这里有一点需要注意的!!!注意的!!!注意的!!!
1、客户端的接口接收参数的时候,需要加上@RequestBody 注解。不然可能会接收不到
2、客户端同步信息的接口,要在相应的配置文件中做一下sso的拦截
当然这一切要保证在 服务端和客户端互通的情况下。
有什么不对的地方,请各位指点!
· --------刚入职场的菜鸟
HttpClientUtil工具类:代码如下:
package com.harme.http;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class HttpClientUtil {
private static PoolingHttpClientConnectionManager cm;
private static String EMPTY_STR = "";
private static String UTF_8 = "UTF-8";
private static void init() {
if (cm == null) {
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(50);// 整个连接池最大连接数
cm.setDefaultMaxPerRoute(5);// 每路由最大连接数,默认值是2
}
}
/**
* 通过连接池获取HttpClient
*
* @return
*/
private static CloseableHttpClient getHttpClient() {
init();
return HttpClients.custom().setConnectionManager(cm).build();
}
/**
*
* @param url
* @return
*/
public static String httpGetRequest(String url) {
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet);
}
public static String httpGetRequest(String url, Map<String, Object> params) throws URISyntaxException {
URIBuilder ub = new URIBuilder();
ub.setPath(url);
ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
ub.setParameters(pairs);
HttpGet httpGet = new HttpGet(ub.build());
return getResult(httpGet);
}
public static String httpGetRequest(String url, Map<String, Object> headers, Map<String, Object> params)
throws URISyntaxException {
URIBuilder ub = new URIBuilder();
ub.setPath(url);
ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
ub.setParameters(pairs);
HttpGet httpGet = new HttpGet(ub.build());
for (Map.Entry<String, Object> param : headers.entrySet()) {
httpGet.addHeader(param.getKey(), String.valueOf(param.getValue()));
}
return getResult(httpGet);
}
public static String httpPostRequest(String url) {
HttpPost httpPost = new HttpPost(url);
return getResult(httpPost);
}
public static String httpPostRequest(String url, String apasRegnfoXml, String attrXml, String formXml) throws Exception {
HttpPost httpPost = new HttpPost(url);
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("apasRegnfoXml", apasRegnfoXml);
paramMap.put("attrXml", attrXml);
paramMap.put("formXml", formXml);
paramMap.put("urlEncoder", false);
ArrayList<NameValuePair> pairs = covertParams2NVPS(paramMap);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));
return getResult(httpPost);
}
public static String httpPostRequest(String url, Map<String, Object> paramMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> pairs = covertParams2NVPS(paramMap);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));
return getResult(httpPost);
}
public static String httpPostRequest(String url, Map<String, Object> headers, Map<String, Object> params)
throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
for (Map.Entry<String, Object> param : headers.entrySet()) {
httpPost.addHeader(param.getKey(), String.valueOf(param.getValue()));
}
ArrayList<NameValuePair> pairs = covertParams2NVPS(params);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, UTF_8));
return getResult(httpPost);
}
private static ArrayList<NameValuePair> covertParams2NVPS(Map<String, Object> params) {
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> param : params.entrySet()) {
pairs.add(new BasicNameValuePair(param.getKey(), String.valueOf(param.getValue())));
}
return pairs;
}
/**
* 处理Http请求
*
* @param request
* @return
*/
private static String getResult(HttpRequestBase request) {
// CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
try {
response = httpClient.execute(request);
// response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
// long len = entity.getContentLength();// -1 表示长度未知
String result = EntityUtils.toString(entity);
// httpClient.close();
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(response != null) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return EMPTY_STR;
}
}