公司项目需要为用户开发一个POST接口,使得用户在不用登陆的状态下访问接口并传输数据。
1.需要传输的数据形式为data={"esealList":[{"id":"1","name":"xiaoming","age":"21"},{...},{...}]}。为了解析这种json类型的字符串,项目使用了GSON来解析数据,GSON的使用需要导入的JAR包这里不做赘述。
2.因为每条数据里的字段涉及到了多个pojo类,所以无法直接把数据转成Java Bean类,而且data还带有请求头,所以需要编写辅助类来帮助GSON对data进行解析。项目中的锁数据涉及到了锁和企业两个pojo类,辅助类如下:
//锁辅助类
private class EsealPostOut {
private List esealList;
public List getEsealList() {
return esealList;
}
public void setEsealList(List esealList) {
this.esealList = esealList;
}
}
//企业辅助类
private class EntPostOut {
private List esealList;
public List getEsealList() {
return esealList;
}
public void setEsealList(List esealList) {
this.esealList = esealList;
}
}
3.编写辅助类用来提示返回信息
private class ResponseCode {
//异常代码
private String code;
//异常信息
private String msg;
//具体异常内容
private T data;
/**
* 10000:成功
* 10001:密钥验证失败;
10002:字段为空;
10003:字段异常(必选为空或字段不规范);
10004:数据处理失败;
*/
static final String SUCCESS_CODE = "10000";
static final String FAILKEY_CODE = "10001";
static final String EMPTYDATA_CODE = "10002";
static final String EXCEPTIONDATA_CODE = "10003";
static final String SYSERROR_CODE = "10004";
static final String SUCCESS_MSG = "操作成功";
static final String FAILKEY_MSG = "密钥验证失败";
static final String EMPTYDATA_MSG = "数据为空";
static final String EXCEPTIONDATA_MSG = "字段异常";
static final String SYSERROR_MSG = "系统异常";
public ResponseCode(){};
public ResponseCode(String code, String msg) {
super();
this.code = code;
this.msg = msg;
}
public ResponseCode(String code, String msg, T data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
public T getData() {
return data;
}
}
4.对data里的数据进行校验,主要校验必选字段和不规范字段
private static void checkEseal(List eseallist, List entlist) {
String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$";
String regexMail = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
String regexPhone = "^[1][3,4,5,7,8][0-9]{9}$";
String regexFax = "^[0][1-9]{2,3}-[0-9]{5,10}$";
for (int i = 0; i < eseallist.size(); i++) {
if("".equals(eseallist.get(i).getEsealId()) || null == eseallist.get(i).getEsealId()) {
list.add("第" + (i + 1) + "条数据的esealId字段为空");
}else{
if(!eseallist.get(i).getEsealId().matches(regex) || eseallist.get(i).getEsealId().length() > 50) {
list.add("第" + (i + 1) + "条数据的esealId字段不符合规范");
}
}
if("".equals(eseallist.get(i).getCustomsCode()) || null == eseallist.get(i).getCustomsCode()) {
list.add("第" + (i + 1) + "条数据的customsCode字段为空");
}else{
if(!eseallist.get(i).getCustomsCode().matches(regex) || eseallist.get(i).getCustomsCode().length() > 20) {
list.add("第" + (i + 1) + "条数据的customsCode字段不符合规范");
}
}
if(!"".equals(eseallist.get(i).getVehicleNo()) && null != eseallist.get(i).getVehicleNo()) {
if(!eseallist.get(i).getVehicleNo().matches(regex) || eseallist.get(i).getVehicleNo().length() > 20) {
list.add("第" + (i + 1) + "条数据的vehicleNo字段不符合规范");
}
}
if(!"".equals(eseallist.get(i).getSimNo()) && null != eseallist.get(i).getSimNo()) {
if(!eseallist.get(i).getSimNo().matches(regex) || eseallist.get(i).getSimNo().length() > 30) {
list.add("第" + (i + 1) + "条数据的simNo字段不符合规范");
}
}
if(!"".equals(eseallist.get(i).getCustomsName()) && null != eseallist.get(i).getCustomsName()) {
if(!eseallist.get(i).getCustomsName().matches(regex) || eseallist.get(i).getCustomsName().length() > 20) {
list.add("第" + (i + 1) + "条数据的customsName字段不符合规范");
}
}
if("".equals(entlist.get(i).getEntName()) || null == entlist.get(i).getEntName()) {
list.add("第" + (i + 1) + "条数据的entName字段为空");
}else{
if(!entlist.get(i).getEntName().matches(regex) || entlist.get(i).getEntName().length() > 20) {
list.add("第" + (i + 1) + "条数据的entName字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getLeadingOfficial()) && null != entlist.get(i).getLeadingOfficial()) {
if(!entlist.get(i).getLeadingOfficial().matches(regex) || entlist.get(i).getLeadingOfficial().length() > 30) {
list.add("第" + (i + 1) + "条数据的leadingOfficial字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getContact()) && null != entlist.get(i).getContact()) {
if(!entlist.get(i).getContact().matches(regex) || entlist.get(i).getContact().length() > 30) {
list.add("第" + (i + 1) + "条数据的contact字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getAddress()) && null != entlist.get(i).getAddress()) {
if(!entlist.get(i).getAddress().matches(regex) || entlist.get(i).getAddress().length() > 250) {
list.add("第" + (i + 1) + "条数据的address字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getMail()) && null != entlist.get(i).getMail()) {
if(!Pattern.compile(regexMail).matcher(entlist.get(i).getMail()).matches() || entlist.get(i).getMail().length() > 50) {
list.add("第" + (i + 1) + "条数据的mail字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getPhone()) && null != entlist.get(i).getPhone()) {
if(!Pattern.compile(regexPhone).matcher(entlist.get(i).getPhone()).matches() || entlist.get(i).getPhone().length() > 20) {
list.add("第" + (i + 1) + "条数据的phone字段不符合规范");
}
}
if(!"".equals(entlist.get(i).getFax()) && null != entlist.get(i).getFax()) {
if(!Pattern.compile(regexFax).matcher(entlist.get(i).getFax()).matches() || entlist.get(i).getFax().length() > 30) {
list.add("第" + (i + 1) + "条数据的fax字段不符合规范");
}
}
}
}
5.编写方法,包括用户密钥key的校验、返回提示的获取以及执行插入或更新操作
Logger log = Logger.getLogger(CopInfoAPI.class);
//定义一个list来存储异常信息
static List list = new ArrayList();
@SystemLog(logName = "POST接口插入或更新锁数据")
@RequestMapping(value="/esealInfoAdd.json", produces = "application/json;charset=utf-8",method=RequestMethod.POST)
@ResponseBody
public ResponseCode doEsealInfoAdd(String key, String data) throws Exception{
//key检测
GbEntKeysInfo k = this.entKeysService.loadByKey(key);
//key不存在,返回失败信息
if(" ".equals(k) || null == k) {
log.error("错误密钥:" + key);
return new ResponseCode(ResponseCode.FAILKEY_CODE, ResponseCode.FAILKEY_MSG, "密钥错误");
}
try{
Gson gson = new Gson();
EsealPostOut record = gson.fromJson(data, EsealPostOut.class);
EntPostOut re = gson.fromJson(data, EntPostOut.class);
//数组长度小于等于0,返回信息
if(record.getEsealList().size() <= 0) {
log.info("key=" + key + "\ndata=" + data);
return new ResponseCode(ResponseCode.EMPTYDATA_CODE, ResponseCode.EMPTYDATA_MSG, null);
}
list.clear();
//数组长度大于0,执行下列操作
checkEseal(record.getEsealList(), re.getEsealList());
//看一下list里有没有异常信息,如果list不为空
if(list.size() > 0) {
log.info("key=" + key + "\ndata=" + data);
return new ResponseCode(ResponseCode.EXCEPTIONDATA_CODE, ResponseCode.EXCEPTIONDATA_MSG, list);
}
for(int i = 0; i < record.getEsealList().size(); i++) {
GbEntInfo ent = this.entService.loadByName(re.getEsealList().get(i).getEntName());
GbEsealInfo eseal = this.esealService.loadByName(record.getEsealList().get(i).getEsealId());
this.entService.saveEsealData(re.getEsealList().get(i), record.getEsealList().get(i), ent, eseal);
}
}catch(Exception e) {
log.info("key=" + key + "\ndata=" + data);
log.error(e.getMessage());
return new ResponseCode(ResponseCode.SYSERROR_CODE, ResponseCode.SYSERROR_MSG, e.getMessage());
}
log.info("key=" + key + "\ndata=" + data);
return new ResponseCode(ResponseCode.SUCCESS_CODE, ResponseCode.SUCCESS_MSG, "SUCCESS");
}
6.为了实现数据库事务,需要新编写service。因为事务控制只能作用到service层,而数据的处理涉及到了多个pojo类,需要保证对多个表进行插入或更新操作时数据是一致的。具体的service的编写不再展示,需要根据自己的需求来写。