Commit 23e98c88 authored by mcb's avatar mcb

no message

parent 7f78a584
......@@ -96,6 +96,13 @@ public class JsonResult<T> implements Serializable {
return result;
}
public static JsonResult<Object> error(String message) {
JsonResult<Object> result = new JsonResult<>();
result.setCode(ResultCode.INTERNAL_SERVER_ERROR);
result.setMessage(message);
return result;
}
public static JsonResult<Object> error(ResultCode code, String message) {
JsonResult<Object> result = new JsonResult<>();
result.setCode(code);
......
package com.jz.common.exception;
import com.jz.common.constant.ResultCode;
public class ServiceException extends Exception {
private static final long serialVersionUID = 1859731705152111160L;
......
package com.jz.common.utils.web;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
......@@ -39,457 +40,442 @@ import java.util.*;
import java.util.Map.Entry;
/**
*
* @author hack2003
* @date 2016-10-18
* @version v1.0
*
* @date 2016-10-18
*/
public class HttpClientUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
/**
* HttpClient连接SSL
*/
public void ssl() {
CloseableHttpClient httpclient = null;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
FileInputStream instream = new FileInputStream(new File(
"d:\\tomcat.keystore"));
try {
// 加载keyStore d:\\tomcat.keystore
trustStore.load(instream, "123456".toCharArray());
} catch (CertificateException e) {
e.printStackTrace();
} finally {
try {
instream.close();
} catch (Exception ignore) {
}
}
// 相信自己的CA和所有自签名的证书
SSLContext sslcontext = SSLContexts
.custom()
.loadTrustMaterial(trustStore,
new TrustSelfSignedStrategy()).build();
// 只允许使用TLSv1协议
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
// 创建http请求(get方式)
HttpGet httpget = new HttpGet(
"https://localhost:8443/myDemo/Ajax/serivceJ.action");
System.out.println("executing request" + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
}
} finally {
response.close();
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @discription:
* @param url 访问地址
* @param token 请求头中存放token
* @param json 请求数据体
* @return
* @date: 2019年10月25日
*/
public static String postJsonData(String url, String token, String json){
LOGGER.info("===================postJsonData start=======================");
LOGGER.info("url:"+url);
LOGGER.info("token:"+token);
LOGGER.info("json:"+json);
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try
{
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
/**
* HttpClient连接SSL
*/
public void ssl() {
CloseableHttpClient httpclient = null;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
FileInputStream instream = new FileInputStream(new File(
"d:\\tomcat.keystore"));
try {
// 加载keyStore d:\\tomcat.keystore
trustStore.load(instream, "123456".toCharArray());
} catch (CertificateException e) {
e.printStackTrace();
} finally {
try {
instream.close();
} catch (Exception ignore) {
}
}
// 相信自己的CA和所有自签名的证书
SSLContext sslcontext = SSLContexts
.custom()
.loadTrustMaterial(trustStore,
new TrustSelfSignedStrategy()).build();
// 只允许使用TLSv1协议
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
// 创建http请求(get方式)
HttpGet httpget = new HttpGet(
"https://localhost:8443/myDemo/Ajax/serivceJ.action");
System.out.println("executing request" + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
}
} finally {
response.close();
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param url 访问地址
* @param token 请求头中存放token
* @param json 请求数据体
* @return
* @discription:
* @date: 2019年10月25日
*/
public static String postJsonData(String url, String token, String json) {
LOGGER.info("===================postJsonData start=======================");
LOGGER.info("url:" + url);
LOGGER.info("token:" + token);
LOGGER.info("json:" + json);
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
httpPost.setHeader("token", token);
StringEntity se = new StringEntity(json,"UTF-8");
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
LOGGER.info("请求开始时间:"+System.currentTimeMillis());
HttpResponse response = httpClient.execute(httpPost);
LOGGER.info("请求结束时间:"+System.currentTimeMillis());
if(response != null)
{
HttpEntity resEntity = response.getEntity();
if(resEntity != null)
{
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex)
{
LOGGER.info("异常捕捉请求结束时间:"+System.currentTimeMillis());
ex.printStackTrace();
LOGGER.info("请求开始时间:" + System.currentTimeMillis());
HttpResponse response = httpClient.execute(httpPost);
LOGGER.info("请求结束时间:" + System.currentTimeMillis());
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
LOGGER.info("异常捕捉请求结束时间:" + System.currentTimeMillis());
ex.printStackTrace();
LOGGER.info(ex.getMessage());
}
}
LOGGER.info("===================postJsonData end=======================");
return result;
return result;
}
/**
* post方式提交表单(模拟用户登录请求)
*/
public void postForm() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(
"http://localhost:8080/myDemo/Ajax/serivceJ.action");
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("username", "admin"));
formparams.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out
.println("--------------------------------------");
System.out.println("Response content: "
+ EntityUtils.toString(httpEntity, "UTF-8"));
System.out
.println("--------------------------------------");
}
} else {
httppost.abort();
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 发送 get请求
*/
public static void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet("http://www.baidu.com/");
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: "
+ entity.getContentLength());
// 打印响应内容
System.out.println("Response content: "
+ EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* post方式提交表单(模拟用户登录请求)
*/
public void postForm() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(
"http://localhost:8080/myDemo/Ajax/serivceJ.action");
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("username", "admin"));
formparams.add(new BasicNameValuePair("password", "123456"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out
.println("--------------------------------------");
System.out.println("Response content: "
+ EntityUtils.toString(httpEntity, "UTF-8"));
System.out
.println("--------------------------------------");
}
} else {
httppost.abort();
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 发送 get请求
*/
public static void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet("http://www.baidu.com/");
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容长度
System.out.println("Response content length: "
+ entity.getContentLength());
// 打印响应内容
System.out.println("Response content: "
+ EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
* @return
*
* @throws IOException
* @throws ClientProtocolException
*/
public static String post(String url,String json)
{
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try
{
/**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
*
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String post(String url, String json) {
LOGGER.info("===================POST request start=======================");
LOGGER.info("url:" + url);
LOGGER.info("json:" + json);
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
StringEntity se = new StringEntity(json,"UTF-8");
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
System.out.println("-------------"+ JSONObject.toJSONString(se));
System.out.println("-------------" + JSONObject.toJSONString(se));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null)
{
HttpEntity resEntity = response.getEntity();
if(resEntity != null)
{
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
LOGGER.info("===================POST request end=======================");
return result;
}
/**
* 带参
* @param url
* @param paramName
* @param json
* @return
*/
public static String post(String url,String paramName,String json)
{
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try
{
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair(paramName, json);
list.add(param1);
/**
* 带参
*
* @param url
* @param paramName
* @param json
* @return
*/
public static String post(String url, String paramName, String json) {
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
List<NameValuePair> list = new LinkedList<>();
BasicNameValuePair param1 = new BasicNameValuePair(paramName, json);
list.add(param1);
// StringEntity se = new StringEntity(json,"UTF-8");
// se.setContentType("application/json");
// se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
// entityParam.setContentType("application/json");
// entityParam.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(entityParam);
HttpResponse response = httpClient.execute(httpPost);
if(response != null)
{
HttpEntity resEntity = response.getEntity();
if(resEntity != null)
{
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
/**
* 提交JSON参数
* @param url
* @param paramName
* @param json
* @return
*/
public static String postJson(String url,String paramName,String json)
{
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try
{
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
StringEntity se = new StringEntity(json,"UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null)
{
HttpEntity resEntity = response.getEntity();
if(resEntity != null)
{
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
public static String dopost(String url, String json) throws ClientProtocolException,IOException {
String result=null;
String APPLICATION_JSON = "application/json";
String charset="UTF-8";
// 将JSON进行UTF-8编码,以便传输中文
String encoderJson = URLEncoder.encode(json, charset);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity stringEntity = new StringEntity(encoderJson);
stringEntity.setContentType("text/json");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,APPLICATION_JSON));
httppost.setEntity(stringEntity);
//System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out.println("Response content: "+ EntityUtils.toString(httpEntity, charset));
result = EntityUtils.toString(httpEntity,charset);
}
} else {
httppost.abort();
}
response.close();
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**http请求资源,json不编码
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String dopostNoEncode(String url, String json) throws ClientProtocolException,IOException {
String result=null;
String APPLICATION_JSON = "application/json";
String charset="UTF-8";
// 将JSON进行UTF-8编码,以便传输中文
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity stringEntity = new StringEntity(json, charset);
stringEntity.setContentType("text/json");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,APPLICATION_JSON));
httppost.setEntity(stringEntity);
//System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
//System.out.println("Response content: "+ EntityUtils.toString(httpEntity, charset));
result = EntityUtils.toString(httpEntity,charset);
}
} else {
httppost.abort();
}
response.close();
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
* @return
*
* @throws IOException
* @throws ClientProtocolException
*/
public static String postJson(String url, com.alibaba.fastjson.JSONObject json)
{
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try
{
httpPost.setEntity(entityParam);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* 提交JSON参数
*
* @param url
* @param paramName
* @param json
* @return
*/
public static String postJson(String url, String paramName, String json) {
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
public static String dopost(String url, String json) throws ClientProtocolException, IOException {
String result = null;
String APPLICATION_JSON = "application/json";
String charset = "UTF-8";
// 将JSON进行UTF-8编码,以便传输中文
String encoderJson = URLEncoder.encode(json, charset);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity stringEntity = new StringEntity(encoderJson);
stringEntity.setContentType("text/json");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httppost.setEntity(stringEntity);
//System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out.println("Response content: " + EntityUtils.toString(httpEntity, charset));
result = EntityUtils.toString(httpEntity, charset);
}
} else {
httppost.abort();
}
response.close();
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* http请求资源,json不编码
*
* @param url
* @param json
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String dopostNoEncode(String url, String json) throws ClientProtocolException, IOException {
String result = null;
String APPLICATION_JSON = "application/json";
String charset = "UTF-8";
// 将JSON进行UTF-8编码,以便传输中文
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
StringEntity stringEntity = new StringEntity(json, charset);
stringEntity.setContentType("text/json");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httppost.setEntity(stringEntity);
//System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
//System.out.println("Response content: "+ EntityUtils.toString(httpEntity, charset));
result = EntityUtils.toString(httpEntity, charset);
}
} else {
httppost.abort();
}
response.close();
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果
*
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static String postJson(String url, com.alibaba.fastjson.JSONObject json) {
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();//设置请求和传输超时时间
......@@ -498,196 +484,200 @@ public class HttpClientUtils {
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null)
{
HttpEntity resEntity = response.getEntity();
if(resEntity != null)
{
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return result;
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* get请求,返回字节数组
*
* @param url
* @param params
* @param headers
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static byte[] doGetByByte(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
LOGGER.info("===================doGetByByte start =======================");
// 参数
StringBuilder paramsBuilder = new StringBuilder(url);
byte[] responseContent = null;
if (params != null && !params.keySet().isEmpty()) {
if (url.indexOf("?") == -1) {
paramsBuilder.append("?");
}
List<NameValuePair> list = new ArrayList<>();
Set<String> keySet = params.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = params.get(key);
list.add(new BasicNameValuePair(key, value));
}
String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(list));
paramsBuilder.append(paramsStr);
}
HttpGet httpGet = new HttpGet(paramsBuilder.toString());
// 头
if (headers != null && !headers.keySet().isEmpty()) {
Set<String> keySet = headers.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = headers.get(key);
httpGet.addHeader(key, value);
}
}
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity resEntity=null;
if (response.getStatusLine().getStatusCode() == 200) { // 请求成功状态
resEntity = response.getEntity();
}
if(resEntity != null){
responseContent = EntityUtils.toByteArray(resEntity);
/**
* get请求,返回字节数组
*
* @param url
* @param params
* @param headers
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public static byte[] doGetByByte(String url, Map<String, String> params, Map<String, String> headers) throws IOException {
LOGGER.info("===================doGetByByte start =======================");
// 参数
StringBuilder paramsBuilder = new StringBuilder(url);
byte[] responseContent = null;
if (params != null && !params.keySet().isEmpty()) {
if (url.indexOf("?") == -1) {
paramsBuilder.append("?");
}
List<NameValuePair> list = new ArrayList<>();
Set<String> keySet = params.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = params.get(key);
list.add(new BasicNameValuePair(key, value));
}
String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(list));
paramsBuilder.append(paramsStr);
}
HttpGet httpGet = new HttpGet(paramsBuilder.toString());
// 头
if (headers != null && !headers.keySet().isEmpty()) {
Set<String> keySet = headers.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
String value = headers.get(key);
httpGet.addHeader(key, value);
}
}
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity resEntity = null;
if (response.getStatusLine().getStatusCode() == 200) { // 请求成功状态
resEntity = response.getEntity();
}
if (resEntity != null) {
responseContent = EntityUtils.toByteArray(resEntity);
// result = EntityUtils.toString(resEntity,charset);
}
} catch (Exception e) {
LOGGER.info("异常捕捉请求结束时间:"+System.currentTimeMillis());
e.printStackTrace();
}
} catch (Exception e) {
LOGGER.info("异常捕捉请求结束时间:" + System.currentTimeMillis());
e.printStackTrace();
LOGGER.info(e.getMessage());
}
LOGGER.info("===================doGetByByte end=======================");
return responseContent;
}
/**
* @discription:
* @param url 访问地址
* @param token 请求头中存放token
* @param json 请求数据体
* @return
* @date: 2019年10月25日
*/
public static byte[] postJsonDataByByte(String url, String token, String json){
LOGGER.info("===================postJsonDataByByte start=======================");
LOGGER.info("url:"+url);
LOGGER.info("token:"+token);
LOGGER.info("json:"+json);
String charset="UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
}
LOGGER.info("===================doGetByByte end=======================");
return responseContent;
}
/**
* @param url 访问地址
* @param token 请求头中存放token
* @param json 请求数据体
* @return
* @discription:
* @date: 2019年10月25日
*/
public static byte[] postJsonDataByByte(String url, String token, String json) {
LOGGER.info("===================postJsonDataByByte start=======================");
LOGGER.info("url:" + url);
LOGGER.info("token:" + token);
LOGGER.info("json:" + json);
String charset = "UTF-8";
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
byte[] responseContent = null;
try
{
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build();//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
httpPost.setHeader("token", token);
StringEntity se = new StringEntity(json,"UTF-8");
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
LOGGER.info("请求开始时间:"+System.currentTimeMillis());
HttpResponse response = httpClient.execute(httpPost);
LOGGER.info("请求结束时间:"+System.currentTimeMillis());
if(response != null){
HttpEntity resEntity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200) {
if(resEntity != null){
responseContent = EntityUtils.toByteArray(resEntity);
}
}else {
LOGGER.info("请求失败:"+EntityUtils.toString(resEntity,charset));
return responseContent;
LOGGER.info("请求开始时间:" + System.currentTimeMillis());
HttpResponse response = httpClient.execute(httpPost);
LOGGER.info("请求结束时间:" + System.currentTimeMillis());
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == 200) {
if (resEntity != null) {
responseContent = EntityUtils.toByteArray(resEntity);
}
} else {
LOGGER.info("请求失败:" + EntityUtils.toString(resEntity, charset));
return responseContent;
}
}
}catch(Exception ex){
LOGGER.info("异常捕捉请求结束时间:"+System.currentTimeMillis());
ex.printStackTrace();
}
} catch (Exception ex) {
LOGGER.info("异常捕捉请求结束时间:" + System.currentTimeMillis());
ex.printStackTrace();
LOGGER.info(ex.getMessage());
}
}
LOGGER.info("===================postJsonDataByByte end=======================");
return responseContent;
return responseContent;
}
/**
* 发送 getJsonForParam请求
*/
public static String getJsonForParam(String url, Map<String, Object> paramMap) {
LOGGER.info("===================getJsonForParam start=======================");
LOGGER.info("url:" + url);
LOGGER.info("json:" + paramMap);
/**
* 发送 getJsonForParam请求
* @author Bellamy
*/
public static String getJsonForParam(String url, Map<String, Object> paramMap) {
LOGGER.info("===================Get request getJsonForParam start=======================");
LOGGER.info("url:" + url);
LOGGER.info("json:" + paramMap);
String result = null;
StringBuilder sb = new StringBuilder();
for (Entry<String, Object> entry : paramMap.entrySet()) {
sb.append("&");
sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
}
String requestUrl = url+sb.toString();
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet(requestUrl);
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
result = EntityUtils.toString(entity);
LOGGER.info(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.info("===================getJsonForParam end=======================");
return result;
}
public static void main(String[] args) throws Exception {
// 私钥
String fh = "";
if (paramMap != null) {
if (url.indexOf("?") == -1) {
fh = "?";
}
for (Entry<String, Object> entry : paramMap.entrySet()) {
sb.append("&");
sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
}
}
if (StringUtils.isNotEmpty(sb)) {
fh += sb.toString().substring(1);
}
String requestUrl = url + fh;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet(requestUrl);
System.out.println("get executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
result = EntityUtils.toString(entity);
LOGGER.info(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.info("===================getJsonForParam end=======================");
return result;
}
public static void main(String[] args) throws Exception {
// 私钥
/*String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJzAfk55exxmvy3+pXsJINZEtmwUp6eBfDSl2YnT5bdJL3nzPnSjmaWsf8x9hR4QyGnvlV/Vo0sk36X7ATcqxfIn0+6W5f8IR4XtVDhxZsD/cK8nVThqFGQagmyNAwxP/wBnAXOy+fpwZrMOgqfosYmVsmImFWbHA87C4mx0bwoJAgMBAAECgYB4tlBOVIT3ITTW0cRT1HrCJxYoc1uMxk2FKbc1ycWceTKjgiu1nQtEp2ufaYYq2hfMZOEudRIUWygT5RFRj5HxLfL6Me3y6dtgyHvOVeMDNGAG+tsn8ObQCQjZ/hVKzFFgHlrHv5i4zX44im2IdvLqnV6cEUneduJfZAQT/XTGUQJBANAZbuTqlDRj/9ObGZEvaPe95FGAPNFEiNmRvLsCsRmruJA5h2ogwx8O4Yll5LylKV6C33Vws4pgAPBHvOzGTx0CQQDA1VYU4ihNyvMknFIAmMT2ojQmQ8ASX64hVFWY9ehf5JaJ+ZD1c1BvbrmubpIo7pPci50BmXnjq6EcxVML/VbdAkAwEH/FjczXYPV4yY0ZNIsZFZoDnQvvBdZZ8khWJWQEWt5RKYh2YcTPip9bHda8H6Wzd6TnOjWt00jENr2TLqadAkEAq0TQD/xOj8mR6xJsQtttFSE78EB8d9VDc5bT7+d5XLJKgoGGnnqtFkvh32uVpYVBDsFx0dneyLfHgSZBfISmgQJADZ71qrfCDvuoYNS4aOW52OL6LMC84Qi2EnZzl5OHkUuOv5jwBoOCRLEn0N999EMP6DBg8P5kg1llTq7bMG11ug==";
RequestCSVFileParamBean bean = new RequestCSVFileParamBean();
bean.setGetFile("1.csv");
......@@ -697,7 +687,7 @@ public class HttpClientUtils {
HttpClientUtils http = new HttpClientUtils();
String url = "http://127.0.0.1:8080/mysales_interface/getExtCsvFile";
http.post(url,JSON.toJSONString(bean));*/
}
}
}
package com.jz.dmp.modules.controller.dataService;
import com.jz.common.constant.JsonResult;
import com.jz.common.constant.ResultCode;
import com.jz.dmp.modules.controller.dataOperation.bean.DataDevTaskListDto;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageAddReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageListQueryReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageUpdateReq;
import com.jz.dmp.modules.service.DmpOrgMangeService;
import com.jz.dmp.modules.service.DvRuleTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 数据服务组织管理
* 数据服务组织管理--调用gateway 接口
*
* @author Bellamy
* @since 2020-12-24 10:56:18
......@@ -17,11 +29,102 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/orgMange")
@Api(tags = "数据服务-组织管理")
public class DmpOrgMangeController {
private static Logger logger = LoggerFactory.getLogger(DmpOrgMangeController.class);
/**
* 服务对象
*/
@Autowired
private DmpOrgMangeService dmpOrgMangeService;
/**
* 列表分页查询
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@ApiOperation(value = "列表分页查询", notes = "列表分页查询")
@PostMapping(value = "/listPage")
public JsonResult getOrgListPage(@RequestBody @Validated OrganizationManageListQueryReq req, HttpServletRequest httpRequest) {
JsonResult jsonResult = new JsonResult();
try {
jsonResult = dmpOrgMangeService.queryOrgListPage(req);
} catch (Exception e) {
jsonResult.setMessage(e.getMessage());
jsonResult.setCode(ResultCode.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
return jsonResult;
}
/**
* 删除组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@ApiOperation(value = "删除组织", notes = "删除组织")
@GetMapping(value = "/delOrg")
@ApiImplicitParam(name = "id",value = "组织id",required = true)
public JsonResult delOrgById(@RequestParam long id, HttpServletRequest httpRequest) {
JsonResult jsonResult = new JsonResult();
try {
jsonResult = dmpOrgMangeService.delOrgById(id);
} catch (Exception e) {
jsonResult.setMessage(e.getMessage());
jsonResult.setCode(ResultCode.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
return jsonResult;
}
/**
* 新增组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@ApiOperation(value = "新增组织", notes = "新增组织")
@PostMapping(value = "/addOrg")
public JsonResult addOrg(@RequestBody @Validated OrganizationManageAddReq req, HttpServletRequest httpRequest) {
if (StringUtils.isEmpty(req.getOrgName())) {
return JsonResult.error(ResultCode.PARAMS_ERROR,"组织名称不能为空!");
}
if (StringUtils.isEmpty(req.getOrgType())) {
return JsonResult.error(ResultCode.PARAMS_ERROR,"组织类型不能为空!");
}
JsonResult jsonResult = new JsonResult();
try {
jsonResult = dmpOrgMangeService.addOrg(req);
} catch (Exception e) {
jsonResult.setMessage(e.getMessage());
jsonResult.setCode(ResultCode.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
return jsonResult;
}
/**
* 编辑组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@ApiOperation(value = "编辑组织", notes = "编辑组织")
@PostMapping(value = "/updateOrg")
public JsonResult updateOrg(@RequestBody @Validated OrganizationManageUpdateReq req, HttpServletRequest httpRequest) {
JsonResult jsonResult = new JsonResult();
try {
jsonResult = dmpOrgMangeService.updateOrg(req);
} catch (Exception e) {
jsonResult.setMessage(e.getMessage());
jsonResult.setCode(ResultCode.INTERNAL_SERVER_ERROR);
e.printStackTrace();
}
return jsonResult;
}
}
\ No newline at end of file
package com.jz.dmp.modules.controller.dataService.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @ClassName: OrganizationManageAddReq
* @Description: 新增组织管理请求体
* @Author: Bellamy
* @Date 2021/1/16
* @Version 1.0
*/
@Data
@ApiModel("新增组织管理详情请求体")
public class OrganizationManageAddReq implements Serializable {
private static final long serialVersionUID = 3131683881168540907L;
@ApiModelProperty(value = "组织类型:INT 内部组织 OUT 外部组织", required = true)
@NotNull(message = "组织类型不能为空!")
@NotEmpty(message = "组织类型不能为空!")
private String orgType;
@ApiModelProperty(value = "组织名称", required = true)
@NotNull(message = "组织名称不能为空")
@NotEmpty(message = "组织名称不能为空!")
private String orgName;
@ApiModelProperty(value = "组织描述", required = false)
private String orgDesc;
@ApiModelProperty(value = "组织英文名称", required = false)
private String orgCnName;
@ApiModelProperty(value = "组织邮箱", required = false)
private String orgMail;
@ApiModelProperty(value = "联系方式", required = true)
@NotEmpty(message = "联系方式不能为空!")
private String orgPhone;
@ApiModelProperty(value = "状态(NORMAL-正常 FREEZE-冻结 CANCEL-注销)", required = true)
private String status;
@ApiModelProperty(value = "联系人", required = true)
private String createUser;
}
package com.jz.dmp.modules.controller.dataService.bean;
import com.jz.common.bean.BasePageBean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @ClassName: DmpOrgMangeService
* @Description: 组织管理列表查询请求体
* @Author: Bellamy
* @Date 2021/1/16
* @Version 1.0
*/
@Data
@ApiModel("组织管理列表查询请求体")
public class OrganizationManageListQueryReq extends BasePageBean implements Serializable {
@ApiModelProperty(value = "组织名称")
private String orgName;
@ApiModelProperty(value = "组织编码(组织唯一标识)")
private String orgCode;
}
package com.jz.dmp.modules.controller.dataService.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @ClassName: OrganizationManageUpdateReq
* @Description: 更新组织管理请求体
* @Author: Bellamy
* @Date 2021/1/16
* @Version 1.0
*/
@Data
@ApiModel("更新组织管理请求体")
public class OrganizationManageUpdateReq implements Serializable {
@ApiModelProperty(value = "组织id",required = true)
@NotNull(message = "组织id不能为空")
private Long id;
/* @ApiModelProperty(value = "组织名称",required = true)
@NotNull(message = "组织名称不能为空")
private String orgName;*/
@ApiModelProperty(value = "组织描述",required = false)
private String orgDesc;
@ApiModelProperty(value = "状态(NORMAL-正常 FREEZE-冻结 CANCEL-注销)",required = false)
private String status;
@ApiModelProperty(value = "组织英文名称",required = false)
private String orgCnName;
@ApiModelProperty(value = "组织邮箱",required = false)
private String orgMail;
@ApiModelProperty(value = "组织电话",required = false)
private String orgPhone;
@ApiModelProperty(value = "备注",required = false)
private String remark;
@ApiModelProperty(value = "创建用户",required = false)
private String updateUser;
}
package com.jz.dmp.modules.service;
import com.jz.common.constant.JsonResult;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageAddReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageListQueryReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageUpdateReq;
/**
* @ClassName: DmpOrgMangeService
* @Description: 数据服务组织管理
......@@ -8,4 +13,40 @@ package com.jz.dmp.modules.service;
* @Version 1.0
*/
public interface DmpOrgMangeService {
/**
* 列表分页查询
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
JsonResult queryOrgListPage(OrganizationManageListQueryReq req) throws Exception;
/**
* 删除组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
JsonResult delOrgById(long id) throws Exception;
/**
* 新增组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
JsonResult addOrg(OrganizationManageAddReq req) throws Exception;
/**
* 编辑组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
JsonResult updateOrg(OrganizationManageUpdateReq req) throws Exception;
}
package com.jz.dmp.modules.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.jz.common.constant.JsonResult;
import com.jz.common.utils.web.HttpClientUtils;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageAddReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageListQueryReq;
import com.jz.dmp.modules.controller.dataService.bean.OrganizationManageUpdateReq;
import com.jz.dmp.modules.service.DmpOrgMangeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName: DmpOrgMangeServiceImpl
* @Description: 数据服务组织管理
......@@ -13,5 +27,119 @@ import org.springframework.stereotype.Service;
@Service("dmpOrgMangeService")
public class DmpOrgMangeServiceImpl implements DmpOrgMangeService {
private static Logger logger = LoggerFactory.getLogger(DmpOrgMangeServiceImpl.class);
//列表分页查询url
private static final String orgListPage = "/api/organization/listOrg";
//删除组织url
private static final String delOrg = "/api/organization/logoutOrg";
//新增组织url
private static final String addOrg = "/api/organization/add";
//编辑组织url
private static final String updateOrg = "/api/organization/update";
@Value("${spring.gateway-url}")
private String gatewayUrl;
/**
* 列表分页查询
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@Override
public JsonResult queryOrgListPage(OrganizationManageListQueryReq req) throws Exception {
JsonResult result = new JsonResult();
String url = gatewayUrl + orgListPage;
if (StringUtils.isNotEmpty(req.getOrgName())) {
req.setOrgName(req.getOrgName().trim());
}
String resultData = HttpClientUtils.post(url, JSONObject.toJSONString(req));
if (StringUtils.isEmpty(resultData)) {
throw new RuntimeException("查询失败!");
}
logger.info("#################组织管理列数据{}" + resultData);
Map jsonObject = JSONObject.parseObject(resultData);
if (jsonObject != null) {
result.setData(jsonObject.get("data"));
/*Map records = JSONObject.parseObject(jsonObject.get("data").toString());
List<Map> list = (List<Map>) records.get("records");*/
}
return result;
}
/**
* 删除组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@Override
public JsonResult delOrgById(long id) throws Exception {
Map params = new HashMap();
params.put("id", id);
String url = gatewayUrl + delOrg;
String returnData = HttpClientUtils.getJsonForParam(url, params);
if (StringUtils.isEmpty(returnData)) {
throw new RuntimeException("删除失败!");
}
logger.info("#################响应结果{}" + returnData);
Map map = JSONObject.parseObject(returnData);
if ("200".equals(map.get("code").toString())) {
return JsonResult.ok();
}
return JsonResult.error("删除失败!");
}
/**
* 新增组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@Override
public JsonResult addOrg(OrganizationManageAddReq req) throws Exception {
String url = gatewayUrl + addOrg;
String returnData = HttpClientUtils.post(url, JSONObject.toJSONString(req));
if (StringUtils.isEmpty(returnData)) {
throw new RuntimeException("新增失败!");
}
logger.info("#################响应结果{}" + returnData);
Map map = JSONObject.parseObject(returnData);
if ("200".equals(map.get("code").toString())) {
return JsonResult.ok();
}
logger.info(map.get("message").toString());
return JsonResult.error(map.get("message").toString());
}
/**
* 编辑组织
*
* @return
* @author Bellamy
* @since 2021-01-16
*/
@Override
public JsonResult updateOrg(OrganizationManageUpdateReq req) throws Exception {
String url = gatewayUrl + updateOrg;
String returnData = HttpClientUtils.post(url, JSONObject.toJSONString(req));
if (StringUtils.isEmpty(returnData)) {
throw new RuntimeException("编辑失败!");
}
logger.info("#################响应结果{}" + returnData);
Map map = JSONObject.parseObject(returnData);
if ("200".equals(map.get("code").toString())) {
return JsonResult.ok();
}
logger.info(map.get("message").toString());
return JsonResult.error(map.get("message").toString());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment