7 changed files with 147 additions and 299 deletions
@ -1,9 +0,0 @@ |
|||
package com.ccsens.page.persist; |
|||
|
|||
|
|||
/** |
|||
* @author wei |
|||
*/ |
|||
public interface IMessageDao { |
|||
|
|||
} |
@ -1,18 +0,0 @@ |
|||
package com.ccsens.page.persist; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.mongodb.core.MongoTemplate; |
|||
import org.springframework.stereotype.Repository; |
|||
|
|||
/** |
|||
* @author wei |
|||
*/ |
|||
@Repository |
|||
public class MessageDao implements IMessageDao { |
|||
private static final String COLLECTION_RAW_MESSAGE = "col_message_raw"; |
|||
private static final String COLLECTION_MESSAGE = "col_message"; |
|||
@Autowired |
|||
private MongoTemplate mongoTemplate; |
|||
|
|||
|
|||
} |
@ -1,155 +0,0 @@ |
|||
package com.ccsens.page.util; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ccsens.page.exception.BaseException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.http.HttpEntity; |
|||
import org.springframework.http.HttpHeaders; |
|||
import org.springframework.http.MediaType; |
|||
import org.springframework.http.ResponseEntity; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.util.LinkedMultiValueMap; |
|||
import org.springframework.util.MultiValueMap; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import javax.annotation.PostConstruct; |
|||
import javax.annotation.Resource; |
|||
import java.net.URI; |
|||
import java.net.URISyntaxException; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
public class RestTemplateUtil { |
|||
|
|||
@Resource |
|||
private RestTemplate restTemplate; |
|||
|
|||
private static RestTemplateUtil util; |
|||
|
|||
@PostConstruct |
|||
public void init(){ |
|||
util = this; |
|||
util.restTemplate = this.restTemplate; |
|||
} |
|||
|
|||
public static Object getForEntity(String url, Map<String, Object> params, Class<?> returnClass) { |
|||
|
|||
if (params != null && !params.isEmpty()) { |
|||
String questionMark = "?"; |
|||
String assignMark = "="; |
|||
String andMark = "&"; |
|||
if (!url.contains(questionMark)) { |
|||
url += questionMark; |
|||
} |
|||
for (String key : params.keySet()) { |
|||
if (url.endsWith(questionMark)) { |
|||
url += key + assignMark +params.get(key); |
|||
} else { |
|||
url += andMark + key + assignMark +params.get(key); |
|||
} |
|||
} |
|||
} |
|||
log.info("url:{}, params:{}", url, params); |
|||
ResponseEntity<String> entity = util.restTemplate.getForEntity(url, String.class); |
|||
log.info("entity:{}",entity); |
|||
return JSONObject.parseObject(entity.getBody(), returnClass); |
|||
} |
|||
|
|||
public static String postBody(String url, Object params) { |
|||
log.info("路径:{}, 参数:{}", url, params); |
|||
HttpHeaders httpHeaders = new HttpHeaders(); |
|||
MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8"); |
|||
httpHeaders.setContentType(type); |
|||
|
|||
JSONObject json = JSON.parseObject(JSON.toJSONString(params)); |
|||
HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(json,httpHeaders); |
|||
URI uri; |
|||
try { |
|||
uri = new URI(url); |
|||
}catch (URISyntaxException e) { |
|||
log.error("转换路径异常", e); |
|||
throw new BaseException(CodeEnum.URL_ERROR); |
|||
} |
|||
|
|||
ResponseEntity<String> response = util.restTemplate.postForEntity(uri, objectHttpEntity, String.class); |
|||
log.info("返回:{}", response); |
|||
return response.getBody(); |
|||
} |
|||
|
|||
public static String postBody(String url, List<? extends Object> params) { |
|||
log.info("路径:{}, 参数:{}", url, params); |
|||
HttpHeaders httpHeaders = new HttpHeaders(); |
|||
MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8"); |
|||
httpHeaders.setContentType(type); |
|||
|
|||
HttpEntity<List<? extends Object>> objectHttpEntity = new HttpEntity<>(params,httpHeaders); |
|||
URI uri; |
|||
try { |
|||
uri = new URI(url); |
|||
}catch (URISyntaxException e) { |
|||
log.error("转换路径异常:{}", e); |
|||
throw new BaseException(CodeEnum.URL_ERROR); |
|||
} |
|||
|
|||
ResponseEntity<String> response = util.restTemplate.postForEntity(uri, objectHttpEntity, String.class); |
|||
log.info("返回:{}", response); |
|||
return response.getBody(); |
|||
} |
|||
public static String postUrlEncode(String url, Object params) { |
|||
log.info("请求路径:{},请求参数:{}", url, params); |
|||
MultiValueMap<String, Object> paramMap; |
|||
if (params == null) { |
|||
paramMap = new LinkedMultiValueMap<>(); |
|||
} else { |
|||
JSONObject json = JSON.parseObject(JSON.toJSONString(params)); |
|||
paramMap = transMultiValueMap(json); |
|||
} |
|||
|
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|||
HttpEntity<MultiValueMap> formEntity = new HttpEntity<>(paramMap, headers); |
|||
ResponseEntity<String> result = util.restTemplate.postForEntity(url, formEntity, String.class); |
|||
log.info("接口返回结果:{}", result); |
|||
return result.getBody(); |
|||
} |
|||
|
|||
/** |
|||
* 发送multipart/form-data |
|||
* @author whj |
|||
* @date 2019/8/20 |
|||
* @param url 路径 |
|||
* @param params 参数 |
|||
* @return com.alibaba.fastjson.JSONObject |
|||
*/ |
|||
public static JSONObject postImg(String url, JSONObject params) { |
|||
log.info("请求路径:{},请求参数:{}", url, params); |
|||
HttpHeaders headers = new HttpHeaders(); |
|||
headers.add("Accept", MediaType.APPLICATION_JSON.toString()); |
|||
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|||
MultiValueMap<String, Object> paramMap = transMultiValueMap(params); |
|||
HttpEntity<MultiValueMap> formEntity = new HttpEntity<>(paramMap, headers); |
|||
JSONObject result = util.restTemplate.postForObject(url, formEntity, JSONObject.class); |
|||
log.info("接口返回结果:{}", result); |
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 将参数封装成MultiValueMap对象 |
|||
* @author whj |
|||
* @date 2019/8/20 |
|||
* @param params 参数 |
|||
* @return org.springframework.util.MultiValueMap<java.lang.String,java.lang.Object> |
|||
*/ |
|||
private static MultiValueMap<String, Object> transMultiValueMap(JSONObject params) { |
|||
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>(); |
|||
for (String key: params.keySet()) { |
|||
paramMap.add(key, params.get(key)); |
|||
} |
|||
return paramMap; |
|||
} |
|||
|
|||
|
|||
} |
Loading…
Reference in new issue