Commit 82414d64 authored by linruibiao's avatar linruibiao

接口编写

parent fd40ee3b
package com.syc.slm.common.core.util;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import com.syc.slm.common.core.constant.SlmConstants;
......@@ -12,11 +11,12 @@ import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Supplier;
/**
* slm-bi
* RestTemplate 远程调用工具类
* RestTemplate 远程调用工具类
*
* @author : lin
* @date : 2021-04-13 10:15
**/
......@@ -25,67 +25,77 @@ public final class RestTemplateUtils {
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
public static <TResult> TResult get(String url, final Type resultType, Map<String, ?> uriVariables) {
return get(url, null, resultType, uriVariables);
}
// public static <TResult>TResult get(String url, Map<String, String> headerMap, final Type resultType, Map<String, ?> uriVariables) {
// try {
// ResponseEntity<String> response = get(url, headerMap, String.class, uriVariables);
// if(HttpStatus.OK.value() == response.getStatusCodeValue()){
// if(resultType.equals(String.class)) {
// return (TResult)response.getBody();
// }
// return SlmConstants.gson.fromJson(response.getBody(), resultType);
// }
// } catch (Exception e) {
// log.error("出错啦,{}",e);
// throw new SysException("出错啦,请联系管理员",e.toString());
// }
// return null;
// }
public static <TResult>TResult get(String url, final Type resultType, Map<String, ?> uriVariables) {
public static <TResult> TResult get(String url, Supplier<Map<String, String>> headerHandler, final Type resultType, Map<String, ?> uriVariables) {
try {
// 获取token以及在请求头加上token
HttpServletRequest request = ServletUtils.getHttpServletRequest();
Map<String, String> headerMap = ServletUtils.getHeaderMap(request);
if(CollectionUtils.isEmpty(headerMap)){
throw new SysException("头部不能为空");
}
String token =request.getHeader(SlmConstants.TOKEN_PREFIX);
if(StringUtils.isBlank(token)){
String token = request.getHeader(SlmConstants.TOKEN_PREFIX);
if (StringUtils.isBlank(token)) {
throw new SysException("token不能为空");
}
Map<String, String> headMap = Maps.newHashMap();
headMap.put(SlmConstants.TOKEN_PREFIX, token);
ResponseEntity<String> response = get(url, headMap, String.class, uriVariables);
log.info("请求结果是=============:{}",response.getBody());
if(HttpStatus.OK.value() == response.getStatusCodeValue()){
if(resultType.equals(String.class)) {
if (headerHandler != null) {
Map<String, String> header = headerHandler.get();
header.forEach(headMap::put);
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headMap);
HttpEntity<?> requestEntity = new HttpEntity<>(httpHeaders);
ResponseEntity<String> response = REST_TEMPLATE.exchange(url, HttpMethod.GET, requestEntity, String.class, uriVariables);
log.info("请求结果是=============:{}", response.getBody());
if (HttpStatus.OK.value() == response.getStatusCodeValue()) {
if (resultType.equals(String.class)) {
return (TResult)response.getBody();
}
return SlmConstants.gson.fromJson(response.getBody(), resultType);
}
} catch (Exception e) {
log.error("出错啦,{}",e);
throw new SysException("出错啦,请联系管理员",e.toString());
log.error("出错啦,{}", e);
throw new SysException("出错啦,请联系管理员", e.toString());
}
return null;
}
public static <TResult>TResult post(String url, Map<String, String> headerMap,Object requestBody, final Type resultType, Map<String, ?> uriVariables) {
public static <TResult> TResult post(String url, Object requestBody, final Type resultType, Map<String, ?> uriVariables) {
return post(url, null, requestBody, resultType, uriVariables);
}
public static <TResult> TResult post(String url, Supplier<Map<String, String>> headerHandler, Object requestBody, final Type resultType, Map<String, ?> uriVariables) {
try {
ResponseEntity<String> response = post(url, headerMap, requestBody,String.class, uriVariables);
if(HttpStatus.OK.value() == response.getStatusCodeValue()){
if(resultType.equals(String.class)) {
// 获取token以及在请求头加上token
HttpServletRequest request = ServletUtils.getHttpServletRequest();
String token = request.getHeader(SlmConstants.TOKEN_PREFIX);
if (StringUtils.isBlank(token)) {
throw new SysException("token不能为空");
}
Map<String, String> headMap = Maps.newHashMap();
headMap.put(SlmConstants.TOKEN_PREFIX, token);
if (headerHandler != null) {
Map<String, String> header = headerHandler.get();
header.forEach(headMap::put);
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headMap);
HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
ResponseEntity<String> response =REST_TEMPLATE.exchange(url, HttpMethod.POST, requestEntity, String.class, uriVariables);
if (HttpStatus.OK.value() == response.getStatusCodeValue()) {
if (resultType.equals(String.class)) {
return (TResult)response.getBody();
}
return SlmConstants.gson.fromJson(response.getBody(), resultType);
}
} catch (Exception e) {
log.error("出错啦,{}",e);
throw new SysException("出错啦,请联系管理员",e.toString());
log.error("出错啦,{}", e);
throw new SysException("出错啦,请联系管理员", e.toString());
}
return null;
}
......@@ -94,13 +104,16 @@ public final class RestTemplateUtils {
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> get(String url,
Map<String, String> headers,
Class<T> responseType,
Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return get(url, httpHeaders, responseType, uriVariables);
......@@ -109,13 +122,16 @@ public final class RestTemplateUtils {
/**
* 带请求头的GET请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param url 请求URL
* @param headers 请求头参数
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> get(String url,
HttpHeaders headers,
Class<T> responseType,
Map<String, ?> uriVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
}
......@@ -123,7 +139,7 @@ public final class RestTemplateUtils {
/**
* POST请求调用方式
*
* @param url 请求URL
* @param url 请求URL
* @param responseType 返回对象类型
* @return
*/
......@@ -131,18 +147,21 @@ public final class RestTemplateUtils {
return REST_TEMPLATE.postForEntity(url, HttpEntity.EMPTY, responseType);
}
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> post(String url,
Map<String, String> headers,
Object requestBody,
Class<T> responseType,
Map<String, ?> uriVariables) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(headers);
return post(url, httpHeaders, requestBody, responseType, uriVariables);
......@@ -151,44 +170,53 @@ public final class RestTemplateUtils {
/**
* 带请求头的POST请求调用方式
*
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> post(String url,
HttpHeaders headers,
Object requestBody,
Class<T> responseType,
Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return post(url, requestEntity, responseType, uriVariables);
}
/**
* 自定义请求头和请求体的POST请求调用方式
*
* @param url 请求URL
* @param url 请求URL
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> post(String url,
HttpEntity<?> requestEntity,
Class<T> responseType,
Map<String, ?> uriVariables) {
return REST_TEMPLATE.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables);
}
/**
* 通用调用方式
*
* @param url 请求URL
* @param method 请求方法类型
* @param url 请求URL
* @param method 请求方法类型
* @param requestEntity 请求头和请求体封装对象
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
public static <T> ResponseEntity<T> exchange(String url,
HttpMethod method,
HttpEntity<?> requestEntity,
Class<T> responseType,
Map<String, ?> uriVariables) {
return REST_TEMPLATE.exchange(url, method, requestEntity, responseType, uriVariables);
}
......
......@@ -61,15 +61,15 @@ public class VariableController extends BaseRestController{
Map<String,String> heads = Maps.newHashMap();
heads.put("token","345aa488a820448dbb069ea4295d2b15");
R<List<PanelTreeNodeVo>> newR = new R<>();
ResponseEntity<Map> rest = RestTemplateUtils.get(
"http://localhost:2400/dc/{appId}/equipment/info/name/pull?token={token}&equipment_info_id={equipment_info_id}&customer_id={customer_id}&q_name={q_name}&search_code={search_code}&search_model={search_model}&warranty_status={warranty_status}&page_num={page_num}&page_size={page_size}",
heads,
Map.class,
maps);
if (rest != null){
return rest.getBody();
}
//
// ResponseEntity<Map> rest = RestTemplateUtils.get(
// "http://localhost:2400/dc/{appId}/equipment/info/name/pull?token={token}&equipment_info_id={equipment_info_id}&customer_id={customer_id}&q_name={q_name}&search_code={search_code}&search_model={search_model}&warranty_status={warranty_status}&page_num={page_num}&page_size={page_size}",
// heads,
// Map.class,
// maps);
// if (rest != null){
// return rest.getBody();
// }
return null;
}
......
......@@ -106,11 +106,10 @@ public class BusinessConfigServiceImpl extends ServiceImpl<BusinessConfigMapper,
}
});
});
Map<String, String> heads = Maps.newHashMap();
heads.put("X-Token", user.getToken());
Map<String, String> uriVariables = Maps.newHashMap();
uriVariables.put("appId", user.getAppId());
R<List<Map<String, Object>>> result = RestTemplateUtils.post(uri, heads,dto, new TypeToken<R<List<Map<String, Object>>>>() {}.getType(), uriVariables);
R<List<Map<String, Object>>> result = RestTemplateUtils.post(uri, null,dto, new TypeToken<R<List<Map<String, Object>>>>() {}.getType(), uriVariables);
return result.detach();
}
......@@ -131,7 +130,11 @@ public class BusinessConfigServiceImpl extends ServiceImpl<BusinessConfigMapper,
uriVariables.put("size", size);
uriVariables.put("name", name);
IPage<String> page = new Page<>(current,size);
R<Map<String,Object>> result = RestTemplateUtils.get(uri, new TypeToken<R<Map<String,Object>>>() {}.getType(), uriVariables);
R<Map<String,Object>> result = RestTemplateUtils.get(uri,()->{
Map<String, String> header = Maps.newHashMap();
header.put("X-Token", currentUser.getToken());
return header;
}, new TypeToken<R<Map<String,Object>>>() {}.getType(), uriVariables);
Map<String, Object> resultMaps = result.detach();
if(CollectionUtils.isNotEmpty(resultMaps)){
page.setTotal(new BigDecimal(resultMaps.get("total")+"").longValue());
......
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