Commit 34946857 hc

1. 接口参数校验规则

2. 代码优化
1 个父辈 44213e31
...@@ -266,6 +266,11 @@ ...@@ -266,6 +266,11 @@
<artifactId>log4j-jul</artifactId> <artifactId>log4j-jul</artifactId>
<version>2.17.0</version> <version>2.17.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies> </dependencies>
......
...@@ -17,15 +17,14 @@ import com.neotel.smfcore.custom.luxsan.api.bean.request.*; ...@@ -17,15 +17,14 @@ import com.neotel.smfcore.custom.luxsan.api.bean.request.*;
import com.neotel.smfcore.custom.luxsan.api.bean.result.*; import com.neotel.smfcore.custom.luxsan.api.bean.result.*;
import com.neotel.smfcore.custom.luxsan.api.bean.util.LuxsanApiResult; import com.neotel.smfcore.custom.luxsan.api.bean.util.LuxsanApiResult;
import com.neotel.smfcore.custom.luxsan.api.enums.LuxsanApiEnum; import com.neotel.smfcore.custom.luxsan.api.enums.LuxsanApiEnum;
import com.neotel.smfcore.custom.luxsan.common.constants.BusinessConst;
import com.neotel.smfcore.custom.luxsan.common.util.RequestURLUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
@Service @Service
...@@ -1006,40 +1005,46 @@ public class LuxsanApi extends DefaultSmfApiListener { ...@@ -1006,40 +1005,46 @@ public class LuxsanApi extends DefaultSmfApiListener {
} }
} }
public static List<GetBoxNGResult> getBoxNGsnList(GetBoxNGsnListRequest request) { public static List<GetBoxNGResult> getBoxNGsnList(Map request) throws ApiException {
return RequestURLUtil.request(BusinessConst.DATA_RESULT_TYPE, request, BusinessConst.POST_METHOD, getBoxNGsnListUrl, GetBoxNGResult.class);
}
public static List<GetBoxNGResult> getBoxNGsnList(GetBoxNGsnListRequest request) throws ApiException {
log.info("getBoxNGsnList接口请求参数为:" + JSON.toJSONString(request)); log.info("getBoxNGsnList接口请求参数为:" + JSON.toJSONString(request));
String resultStr = null; String resultStr = null;
// 调用外部接口
try { try {
resultStr = HttpHelper.postJson(getBoxNGsnListUrl, request); resultStr = HttpHelper.postJson(getBoxNGsnListUrl, request);
}catch (Exception e) {
log.error(String.format("接口请求失败[%s]", getBoxNGsnListUrl));
throw new ValidateException("smfcore.api.error", "接口请求失败[{0}]", new String[]{getBoxNGsnListUrl});
}
log.info("getBoxNGsnList接口返回结果为:" + resultStr); log.info("getBoxNGsnList接口返回结果为:" + resultStr);
JSONObject resultJson = JsonUtil.toObj(resultStr, JSONObject.class); JSONObject resultJson = JsonUtil.toObj(resultStr, JSONObject.class);
if (resultJson != null) { String data = resultJson.getString("data");
if (resultJson.getString("data") != null) { if (StringUtils.isEmpty(data)) {
throw new ApiException(String.format("接口返回内容有误[%s]", getBoxNGsnListUrl));
}
JSONObject dataJson = resultJson.getJSONObject("data"); JSONObject dataJson = resultJson.getJSONObject("data");
// 远程接口返回的数据错误
if (dataJson.get("result") != null && !dataJson.getBoolean("result")) { if (dataJson.get("result") != null && !dataJson.getBoolean("result")) {
String errorMessage = (String) dataJson.get("message"); String errorMessage = (String) dataJson.get("message");
if (StringUtils.isEmpty(errorMessage)) { if (StringUtils.isEmpty(errorMessage)) {
errorMessage = ""; errorMessage = "";
} }
log.error(String.format("接口请求失败[%s],失败原因[%s]", getBoxNGsnListUrl, errorMessage)); log.error(String.format("接口请求失败[%s],失败原因[%s]", getBoxNGsnListUrl, errorMessage));
throw new ValidateException("smfcore.api.error", "接口请求失败[{0}],失败原因[{1}]", new String[]{"getBoxNGsnList", errorMessage}); throw new ApiException(String.format("smfcore.api.error 接口请求失败[%s],失败原因[%s]", getBoxNGsnListUrl, errorMessage));
} else { } else {
JSONArray arrayData = dataJson.getJSONArray("data"); JSONArray arrayData = dataJson.getJSONArray("data");
System.out.println(arrayData); System.out.println(arrayData);
List<GetBoxNGResult> resultList = JSONObject.parseArray(JSONArray.toJSONString(arrayData), GetBoxNGResult.class); List<GetBoxNGResult> resultList = JSONObject.parseArray(JSONArray.toJSONString(arrayData), GetBoxNGResult.class);
if (resultList != null && !resultList.isEmpty()) { if (resultList != null && !resultList.isEmpty()) {
return resultList; return resultList;
}else {
return new ArrayList<>();
} }
} }
} }
}
} catch (ApiException e) {
log.error(String.format("接口请求失败[%s]", e.getMessage()));
throw new ValidateException("smfcore.api.error", "接口请求失败[{0}]", new String[]{e.getMessage()});
}
log.error(String.format("接口请求失败[%s]", getBoxNGsnListUrl));
throw new ValidateException("smfcore.api.error", "[{0}]数据获取失败", new String[]{"getBoxNGsnList"});
}
public static void ticketPick(TicketPickRequest request) { public static void ticketPick(TicketPickRequest request) {
......
package com.neotel.smfcore.custom.luxsan.api.bean.request; package com.neotel.smfcore.custom.luxsan.api.bean.request;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank;
/** /**
* 获取料箱ngsn列表 * 获取料箱ngsn列表
*/ */
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
public class GetBoxNGsnListRequest { public class GetBoxNGsnListRequest {
/** /**
* 机种 * 机种
*/ */
@NotBlank(message = "机种不能为空")
private String model; private String model;
/** /**
* 料箱号 * 料箱号
*/ */
@NotBlank(message = "料箱号不能为空")
private String carton_id; private String carton_id;
} }
package com.neotel.smfcore.custom.luxsan.common.constants;
public class BusinessConst {
/*================ HTTP类型 ==========================*/
public static String GET_METHOD = "GET";
public static String POST_METHOD = "POST";
/*================ 解析外部接口类型规则 ==========================*/
/**
* result为 true 时才代表data可用
* eg:
* {
* "code": 1,
* "message": "success",
* "data": {
* "result": "false",
* "message": "箱号中未获取到符合要求的SN信息",
* "data": ""
* },
* "timestamp": 1719205385950,
* "executeTime": 90
* }
*
* {
* "code": 1,
* "message": "success",
* "data": {
* "result": "true",
* "message": "success",
* "data": [{
* "SN": "SN1",
* "NGREASON": "test",
* "ROW": 1,
* "COLUMN": 1
* }, {
* "SN": "SN2",
* "NGREASON": "test",
* "ROW": 15,
* "COLUMN": 23
* }, {
* "SN": "SN3",
* "NGREASON": "test",
* "ROW": 15,
* "COLUMN": 24
* }]
* },
* "timestamp": 1719194526321,
* "executeTime": 20
* }
*/
public static String DATA_RESULT_TYPE = "DataResult";
}
package com.neotel.smfcore.custom.luxsan.common.json;
import com.neotel.smfcore.common.exception.ApiException;
import java.util.List;
import java.util.Map;
/**
* 解析外部接口返回的json规则
*/
public interface IJsonRule {
String getType();
List request(Map request, String httpMethod, String url, Class<?> returnType) throws ApiException;
}
package com.neotel.smfcore.custom.luxsan.common.json.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neotel.smfcore.common.exception.ApiException;
import com.neotel.smfcore.common.exception.ValidateException;
import com.neotel.smfcore.common.utils.HttpHelper;
import com.neotel.smfcore.common.utils.JsonUtil;
import com.neotel.smfcore.common.utils.StringUtils;
import com.neotel.smfcore.custom.luxsan.common.constants.BusinessConst;
import com.neotel.smfcore.custom.luxsan.common.json.IJsonRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class DataResultJsonRule implements IJsonRule {
@Override
public String getType() {
return BusinessConst.DATA_RESULT_TYPE;
}
@Override
public List request(Map request, String httpMethod, String url, Class<?> returnType) throws ApiException {
log.info(String.format("[%s]接口请求参数为:" + JSON.toJSONString(request), url));
String resultStr = null;
// 调用外部接口
try {
if (StringUtils.isEmpty(httpMethod)) {
throw new ApiException("500: 代码错误");
}
switch (httpMethod.toUpperCase()) {
case "GET" :
resultStr = HttpHelper.sendGet(url);
break;
case "POST":
resultStr = HttpHelper.postJson(url, request);
break;
default:
if (StringUtils.isEmpty(httpMethod)) {
throw new ApiException("500: 代码错误");
}
}
}catch (Exception e) {
log.error(String.format("接口请求失败[%s]", url));
throw new ValidateException("smfcore.api.error", "接口请求失败[{0}]", new String[]{url});
}
log.info(String.format("[%s]接口返回结果为:" + resultStr, url));
JSONObject resultJson = JsonUtil.toObj(resultStr, JSONObject.class);
String data = resultJson.getString("data");
if (StringUtils.isEmpty(data)) {
throw new ApiException(String.format("[%s]接口返回内容有误", url));
}
JSONObject dataJson = resultJson.getJSONObject("data");
// 远程接口返回的数据错误
if (dataJson.get("result") != null && !dataJson.getBoolean("result")) {
String errorMessage = (String) dataJson.get("message");
if (StringUtils.isEmpty(errorMessage)) {
errorMessage = "";
}
log.error(String.format("接口请求失败[%s],失败原因[%s]", url, errorMessage));
throw new ApiException(String.format("smfcore.api.error 接口请求失败[%s],失败原因[%s]", url, errorMessage));
} else {
JSONArray arrayData = dataJson.getJSONArray("data");
List resultList = JSONObject.parseArray(JSONArray.toJSONString(arrayData), returnType);
System.out.println(arrayData);
if (resultList != null && !resultList.isEmpty()) {
return resultList;
}else {
return new ArrayList<>();
}
}
}
}
package com.neotel.smfcore.custom.luxsan.common.util;
import com.neotel.smfcore.common.exception.ApiException;
import com.neotel.smfcore.custom.luxsan.common.json.IJsonRule;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class RequestURLUtil {
private static Map<String, IJsonRule> iJsonRuleMap = new HashMap<>();
public RequestURLUtil(List<IJsonRule> iJsonRules) {
for (IJsonRule iJsonRule : iJsonRules) {
iJsonRuleMap.put(iJsonRule.getType(), iJsonRule);
}
}
public static List request(String type, Map request, String httpMethod, String url, Class<?> returnType) throws ApiException {
IJsonRule iJsonRule = iJsonRuleMap.get(type);
List res = iJsonRule.request(request, httpMethod, url, returnType);
return res;
}
}
...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; ...@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.neotel.smfcore.common.bean.ReelLockPosInfo; import com.neotel.smfcore.common.bean.ReelLockPosInfo;
import com.neotel.smfcore.common.bean.ResultBean; import com.neotel.smfcore.common.bean.ResultBean;
import com.neotel.smfcore.common.exception.ApiException;
import com.neotel.smfcore.common.utils.ReelLockPosUtil; import com.neotel.smfcore.common.utils.ReelLockPosUtil;
import com.neotel.smfcore.common.utils.StringUtils; import com.neotel.smfcore.common.utils.StringUtils;
import com.neotel.smfcore.core.barcode.service.manager.IBarcodeManager; import com.neotel.smfcore.core.barcode.service.manager.IBarcodeManager;
...@@ -43,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -43,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -346,9 +348,13 @@ public class LineController { ...@@ -346,9 +348,13 @@ public class LineController {
@ApiOperation("ng料出库") @ApiOperation("ng料出库")
@RequestMapping("/getBoxNGsnList") @RequestMapping("/getBoxNGsnList")
@AnonymousAccess @AnonymousAccess
public ResultBean getBoxNGsnList(@RequestBody GetBoxNGsnListRequest result){ public ResultBean getBoxNGsnList(@Validated @RequestBody GetBoxNGsnListRequest result) throws ApiException {
log.info("机种["+result.getModel()+"]和CartonId:" + result.getCarton_id()); log.info("机种["+result.getModel()+"]和CartonId:" + result.getCarton_id());
List<GetBoxNGResult> boxNGsnList = LuxsanApi.getBoxNGsnList(result); // List<GetBoxNGResult> boxNGsnList = LuxsanApi.getBoxNGsnList(result);
Map<String, String> map = new HashMap<>();
map.put("model", result.getModel());
map.put("carton_id", result.getCarton_id());
List<GetBoxNGResult> boxNGsnList = LuxsanApi.getBoxNGsnList(map);
return ResultBean.newOkResult(boxNGsnList); return ResultBean.newOkResult(boxNGsnList);
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!