HttpRequestUtil
import cn.hutool.http.HttpRequest;
import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public final class HttpRequestUtil {
public final static String get(String url,Map header,Map param){
return request(RequestTypeEnum.GET,url,header,param);
}
public final static String post(String url,Map header,Map param){
return request(RequestTypeEnum.POST,url,header,param);
}
public final static String request(RequestTypeEnum typeEnum, String url, Map header,Map param){
String result = "";
HttpRequest request = null;
if(RequestTypeEnum.GET.equals(typeEnum)){
request = HttpRequest.get(url);
}else {
request = HttpRequest.post(url);
}
if(header!=null){
for(String key : header.keySet()){
request.header(key,header.get(key));
}
}
result = request.body(JSONObject.fromObject(param).toString())
.execute().body();
return result;
}
}
ObjectToMapUtil
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public final class ObjectToMapUtil {
public final static Map objectToMapFirstUp(Object object){
Map dataMap = new HashMap<>();
Class> clazz = object.getClass();
for (Field field : clazz.getDeclaredFields()) {
try {
field.setAccessible(true);
String key = field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
dataMap.put(key,field.get(object));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return dataMap;
}
public final static Map objectToMap(Object object){
Map dataMap = new HashMap<>();
Class> clazz = object.getClass();
for (Field field : clazz.getDeclaredFields()) {
try {
field.setAccessible(true);
String key = field.getName();
dataMap.put(key,field.get(object));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return dataMap;
}
}
RequestTypeEnum
public enum RequestTypeEnum {
POST("POST"),GET("GET");
RequestTypeEnum(String name){
this.name = name;
}
private String name;
public String getName() {
return name;
}
}
接口示例代码
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.http.HttpRequestUtil;
import com.jeeplus.modules.http.ObjectToMapUtil;
import com.jeeplus.modules.http.wms.entity.*;
import com.jeeplus.modules.mes.business.baogong.erporder.entity.BusinessProductionOrderErp;
import com.jeeplus.modules.mes.business.baogong.erporder.service.BusinessProductionOrderErpService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
/**
* WMS完工入库通知 产品入库通知
* @date 2023/7/16
*/
@Service
public class WMSCompleteInStoreApi {
@Autowired
private WSMConfig wsmConfig;
@Autowired
private BusinessProductionOrderErpService businessProductionOrderErpService;
public AjaxJson doCompleteInStore(String orgcode, String mescode, String ordercode, String cinvcode, int num, String unitcode, Date date,String devNo){
AjaxJson json = new AjaxJson();
try {
//查客户和部门
BusinessProductionOrderErp ordererp = businessProductionOrderErpService.getAgDeByCo(orgcode, ordercode);
//传输
WmsCompleteInStoreNoticeRequest wmsCompleteInStoreNoticeRequest= new WmsCompleteInStoreNoticeRequest();
wmsCompleteInStoreNoticeRequest.setRequestType("LIP_3_0301")
.setUserId(wsmConfig.getUserid()).setToken(wsmConfig.getToken());
WmsCompleteInStoreNoticeBean wmsCompleteInStoreNoticeBean = new WmsCompleteInStoreNoticeBean();
WmsCompleteInstoreNoticeHead head = new WmsCompleteInstoreNoticeHead();
head.setSrcBillNo(mescode) //来源单号 (唯一) (编码)
.setSrcBillType("MO04") //来源单类型
.setfstBillNo(ordercode)
.setAgentCode(ordererp.getCustomercode()) //客户编码
.setAgentName(ordererp.getCustomername())
.setDepartCode(ordererp.getDeptcode()) //部门编码
.setDepartName(ordererp.getDeptname())
.setSrcSystem("MES"); //来源系统 填 MES
wmsCompleteInStoreNoticeBean.setHead(head);
WmsCompleteInstoreNoticeDetail detail = new WmsCompleteInstoreNoticeDetail();
detail.setOrgCode(orgcode) //组织编码
.setSkuCode(cinvcode) //货品编码
.setEstMainQty(num) //数量
.setMainUnit(ordererp.getUnitcode()) //单位编码
.setLotAtt01(DateUtils.formatDate(date, "yyyy-MM-dd")) //生产日期 2023-08-25
.setSrcRowId(mescode); //必填(标准接口),随便填吧
// .setFstRowNo(ordercode); //生产订单号
WmsCompleteInstoreNoticePkg pkg = new WmsCompleteInstoreNoticePkg();
pkg.setSrcRowId(mescode)
.setDevNo(devNo) //托盘料号
.setLpn(mescode); //托盘码
wmsCompleteInStoreNoticeBean.getDetail().add(detail);
wmsCompleteInStoreNoticeBean.getPkg().add(pkg);
wmsCompleteInStoreNoticeRequest.setData(wmsCompleteInStoreNoticeBean);
System.out.println(JSONObject.fromObject(wmsCompleteInStoreNoticeRequest).toString());//
Map map = ObjectToMapUtil.objectToMap(wmsCompleteInStoreNoticeRequest);
String result = HttpRequestUtil.post(wsmConfig.getUrl(),null,map);
JSONObject rsJson = JSONObject.fromObject(result);
System.out.println(rsJson.toString());//
String code = rsJson.getString("code");
String message = rsJson.getString("message");
if(!"200".equals(code)){
json.setSuccess(false);
json.setMsg(message);
return json;
}
JSONObject dataJson = rsJson.getJSONObject("data");
dataJson = dataJson.getJSONObject("head");
json.put("wmscode",dataJson.getString("code"));
json.setSuccess(true);
json.setMsg(message);
}catch (Exception e){
e.printStackTrace();
json.setMsg("WMS完工入库通知接口调用失败");
json.setSuccess(false);
}
return json;
}
public static void main(String[] args) {
String result = "{\"code\":\"200\",\"message\":\"操作成功\",\"data\":{\"head\":{\"srcBillNo\":\"WGRK003\",\"srcBillType\":\"MO04\",\"code\":\"23072711171104389\"}}}";
JSONObject rsJson = JSONObject.fromObject(result);
String code = rsJson.getString("code");
String message = rsJson.getString("message");
JSONObject dataJson = rsJson.getJSONObject("data");
dataJson = dataJson.getJSONObject("head");
System.out.println(dataJson.getString("code"));
}
}
实体类
WmsCompleteInStoreNoticeBean
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.List;
/**
* 完工入库通知
* @date 2023/7/24
*/
public class WmsCompleteInStoreNoticeBean implements Serializable {
private WmsCompleteInstoreNoticeHead head;
private List detail = Lists.newArrayList();
private List pkg = Lists.newArrayList();
public WmsCompleteInstoreNoticeHead getHead() {
return head;
}
public WmsCompleteInStoreNoticeBean setHead(WmsCompleteInstoreNoticeHead head) {
this.head = head;
return this;
}
public List getDetail() {
return detail;
}
public WmsCompleteInStoreNoticeBean setDetail(List detail) {
this.detail = detail;
return this;
}
public List getPkg() {
return pkg;
}
public WmsCompleteInStoreNoticeBean setPkg(List pkg) {
this.pkg = pkg;
return this;
}
}
WmsCompleteInstoreNoticeDetail
import java.io.Serializable;
/**
*/
public class WmsCompleteInstoreNoticeDetail implements Serializable {
private String orgCode; // 组织编码
private String skuCode; // 货品编码 *
private String mainUnit;// 主单位编码 *
private Integer estMainQty; // 主单位数量 *
private String srcRowId; // 来源单行ID *
private String fstRowNo; // 来源单行号 *
private String lotAtt01; // 生产日期
}
WmsCompleteInstoreNoticeHead
private String srcBillNo; // 来源单号 *
private String srcBillType; // 来源单类型 *
private String srcSystem;// 来源系统 *
private String fstBillNo;// 源头单号 *
private String agentCode;// 客户编码 //S-00190
private String agentName;// 客户名称 //163
//备料条码号(R00307条码 的 品名 ps:705911781122)
private String departCode; //部门编码
private String departName; //部门名称
WmsCompleteInstoreNoticePkg
private String srcRowId; //来源单行ID
private String lpn; //托盘码
private String devNo; //托盘料号
WmsCompleteInStoreNoticeRequest
import com.jeeplus.common.utils.DateUtils;
import java.io.Serializable;
import java.util.UUID;
/**
* @author
* @date 2023/7/24
*/
public class WmsCompleteInStoreNoticeRequest implements Serializable {
private String requestType; // 请求接口编号 必填,参见接口清单-接口编号
private String requestId; // 请求唯一码 必填,UUID,为防止重复请求
private String requestTime;//请求时间戳 必填,格式:2023-06-09 12:00:00
private String systemId; // 请求系统ID 必填,参照枚举信息-来源系统
private String userId; // 请求用户ID 必填,由WMS进行分配
private String token;// 请求认证码 必填,由WMS进行分配
private String signature;// 请求签名 可选,lower(md5(应用身份ID + data + 应用加密密钥))
private WmsCompleteInStoreNoticeBean data;
public WmsCompleteInStoreNoticeRequest(){
this.requestId = UUID.randomUUID().toString();
this.requestTime = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
this.systemId = "MES";
}
public static WmsCompleteInStoreNoticeRequest init(String requestType, String userId, String token){
WmsCompleteInStoreNoticeRequest common = new WmsCompleteInStoreNoticeRequest();
common.setRequestType(requestType);
common.setUserId(userId)
.setToken(token);
return common;
}
public String getRequestType() {
return requestType;
}
public WmsCompleteInStoreNoticeRequest setRequestType(String requestType) {
this.requestType = requestType;
return this;
}
public String getRequestId() {
return requestId;
}
public WmsCompleteInStoreNoticeRequest setRequestId(String requestId) {
this.requestId = requestId;
return this;
}
public String getRequestTime() {
return requestTime;
}
public WmsCompleteInStoreNoticeRequest setRequestTime(String requestTime) {
this.requestTime = requestTime;
return this;
}
public String getSystemId() {
return systemId;
}
public WmsCompleteInStoreNoticeRequest setSystemId(String systemId) {
this.systemId = systemId;
return this;
}
public String getUserId() {
return userId;
}
public WmsCompleteInStoreNoticeRequest setUserId(String userId) {
this.userId = userId;
return this;
}
public String getToken() {
return token;
}
public WmsCompleteInStoreNoticeRequest setToken(String token) {
this.token = token;
return this;
}
public String getSignature() {
return signature;
}
public WmsCompleteInStoreNoticeRequest setSignature(String signature) {
this.signature = signature;
return this;
}
public WmsCompleteInStoreNoticeBean getData() {
return data;
}
public WmsCompleteInStoreNoticeRequest setData(WmsCompleteInStoreNoticeBean data) {
this.data = data;
return this;
}
}