实体的封装设计思路
思路一:
1,请求实体,返回实体统一封装,业务数据放在data,packet字段中,其他是公共字段,有利于数据的统一解析(josn-->实体)
状态码用于返回状态
消息用来返回调用信息
业务码用法标记调用业务---利于获取对应的类名,方法名,反射调用---利于代码用设计模式
请求地址
public class FacadeSendRequest {
private String applyNo;
private String bankType;
private String insureChannel;
private String tradeCode;
private String packet; //json数据 业务数据
private String url; //请求地址
}
public class FacadeReceiveRequest {
private String bankType;
private String insureChannel;
private String tradeCode;
private String packet; //银行返回的原报文
private String insureApplyNo; //银行返回的原报文
}
public class ApiResponse implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 错误码
*/
private String code = CodeEnum.CODE_0001.getCode();
/**
* 错误描述信息
*/
private String msg = CodeEnum.CODE_0001.getMsg();
/**
* vo 对象
*/
public Object data; //业务数据
}
2,封装格式的拼接,与data的剥离
AbstractFacadeExchange
在一个公共的方法中剥离data
public String callBack(FacadeReceiveRequest facadeReceiveRequest)throws Exception{
String result=null;
try {
String jsonString = JSONObject.toJSONString(facadeReceiveRequest);
String url = apiUrl; //默认回调地址
if(NotifyMethodConstant.INSURE_NOTIFY.equals(facadeReceiveRequest.getTradeCode())){ //投保回调
url = apiInsureUrl;
}
result=HttpClientUtil.httpJsonPost(url, jsonString);
if (StringUtils.isNotBlank(result)){
JSONObject jsonObject = JSONObject.parseObject(result);
if (CodeEnum.CODE_0000.getCode().equals(jsonObject.get("code"))){
result=jsonObject.getString("data");
}
}
} catch (Exception e){
LOGGER.error("请求内部网关异常",e);
}
return result;
}
返回数据的拼接:
BankCallbackController
@RequestMapping("callback/insure")
@ResponseBody
public ApiResponse insureCallback(@Valid @RequestBody FacadeReceiveRequest facadeReceiveRequest, Errors errors) throws Exception {
//验证参数
checkError(errors);
//业务逻辑
ApiResponse apiResponse = new ApiResponse();
try {
String result = insureCallBackService.callback(facadeReceiveRequest);
apiResponse.success();
apiResponse.setData(result);
} catch (Exception e) {
HMonitor.add("bank.callback:exception");
LOGGER.error("银行回调处理异常,参数[{}]", facadeReceiveRequest.toString(), e);
apiResponse.error();
}
return apiResponse;
}
思路二:
当然实体的封装也有另一种思路就是一个抽象父类(公共字段),后面的业务字段类(业务字段)继承抽象父类----这种设计没有上面设计的简单