java-mall/client/src/main/java/com/small/client/Utils/HttpUtils.java

104 lines
4.1 KiB
Java

package com.small.client.Utils;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.small.client.dto.StoreDbConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Slf4j
public class HttpUtils {
public static final String SUCCESSCODE="0";//上传文件
public static final String URL_UPLOUP="/shop/sync/third/uploudSxData";//上传文件
public static final String URL_SYNC_CATEGORY="/shop/sync/third/goods/category";//商品分类数据同步
public static final String URL_SYNC_BRAND="/shop/sync/third/goods/brand";//商品品牌数据同步
public static final String URL_SYNC_GOODS="/shop/sync/third/goods";//商品数据同步
public static final String URL_SYNC_MEMBER="/shop/sync/third/member";
public static final String URL_SYNC_GOODS_READ="/shop/sync/third/readSxData";//商品数据同步
public static final String URL_SYNC_GET_APPSIGN="/shop/sync/third/getAppSign";//获取密文
public static final String URL_SYNC_GET_DOWNCLIENTJAR="/shop/sync/app/downClientJar";//文件下载
public static final String URL_SYNC_GET_STOREdBCONFIG="/shop/sync/third/getStoreDbConfig";//文件下载
public static final String URL_SYNC_GET_STOR_DATA_RELEASE="/shop/sync/third/syncStoreDataRelease";//库存同步
public static String postData(RestTemplate restTemplate, String url,Object modelObject){
// 创建表单参数
// MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
// map.add("key1", "value1");
// map.add("key2", "value2");
// 设置Content-Type为application/x-www-form-urlencoded
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
log.info(modelObject.toString());
HttpEntity<Object> request = new HttpEntity<>(modelObject, headers);
// 发送POST请求
JSONObject jsonObject = restTemplate.postForObject(url, request, JSONObject.class);
assert jsonObject != null;
log.info(jsonObject.toString());
return jsonObject.getStr("error_code");
}
public static String postData(RestTemplate restTemplate, String url,Object modelObject,String contentName){
// 创建表单参数
// MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
// map.add("key1", "value1");
// map.add("key2", "value2");
// 设置Content-Type为application/x-www-form-urlencoded
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
log.info(modelObject.toString());
HttpEntity<Object> request = new HttpEntity<>(modelObject, headers);
// 发送POST请求
JSONObject jsonObject = restTemplate.postForObject(url, request, JSONObject.class);
assert jsonObject != null;
log.info(jsonObject.toString());
if("OK".equals(jsonObject.get("resultCode"))){
return jsonObject.getStr(contentName);
}
return null;
}
public static StoreDbConfig postDataGetConfig(RestTemplate restTemplate, String url, Object modelObject){
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
log.info(modelObject.toString());
HttpEntity<Object> request = new HttpEntity<>(modelObject, headers);
// 发送POST请求
JSONObject jsonObject = restTemplate.postForObject(url, request, JSONObject.class);
assert jsonObject != null;
log.info(jsonObject.toString());
if(0==jsonObject.getInt("error_code")){
JSONObject object= jsonObject.getJSONObject("result");
if(null!=object){
return JSONUtil.toBean(object,StoreDbConfig.class);
}
}
return null;
}
}