Commit 1e68e2b7 by s_guodong

定制vo

parent 1f0bd7b7
package com.ceb.gjjs.mda.special.vo;
import com.brilliance.mda.runtime.annotation.RelPath;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.util.List;
/**
* @Description
* @Author s_guodong
* @Date 2023/8/11
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DemoResultVo extends RespondBaseVo {
@RelPath("acdgrp_rec_account")
private String acount;
private String b;
@RelPath("dataList")
private List<DemoListVo> data_list;
@RelPath("acd")
private DemoVo_2 acd_data;
@RelPath("addrs")
private List<String> addrLines;
}
......@@ -19,22 +19,19 @@ import java.util.List;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class DemoVo implements Serializable {
public class DemoVo extends RequestBaseVo implements Serializable {
@JsonIgnore
private static final long serialVersionUID = 1L;
@Length(max = 2, message = "最大长度为20")
@RelPath("acdgrp_rec_account") // 映射到对应交易vo的字段
@RelPath("acdgrp_rec_account")
private String a;
@Length(max = 2, message = "最大长度为2")
@Need
private String b;
@RelPath("src")
private String src;
@RelPath("dataList")
@Need
private List<DemoListVo> list;
......
package com.ceb.gjjs.mda.special.vo;
import com.brilliance.mda.runtime.annotation.Need;
import com.brilliance.mda.runtime.annotation.RelPath;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
/**
* @Description 请求定制vo的基类
* @Author s_guodong
* @Date 2023/8/10
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class RequestBaseVo {
@Length(max = 8, message = "长度最大8")
@Need
private String id;
@Length(max = 10, message = "长度最大10")
@Need
private String cifno;
@Length(max = 8, message = "长度最大8")
@Need
private String userid;
@RelPath("src")
@Length(max = 12, message = "长度最大12")
@Need
private String src;
}
package com.ceb.gjjs.mda.special.vo;
/**
* @Description 返回定制vo的基类
* @Author s_guodong
* @Date 2023/8/10
*/
public class RespondBaseVo {
}
......@@ -10,6 +10,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
......@@ -21,8 +22,8 @@ import java.util.List;
public class ReflectUtil {
private static Logger log = LoggerFactory.getLogger(ReflectUtil.class);
public static String setObjValue(Object specialVoValue, Class specialVoClass, Object req, Class reqClass) {
Field[] declaredFields = specialVoClass.getDeclaredFields();
public static String setValue2RequestVo(Object requestVo, Class requestVoClass, Object baseVo, Class baseVoClass) {
Field[] declaredFields = requestVoClass.getDeclaredFields();
for (Field f : declaredFields) {
f.setAccessible(true);
......@@ -30,13 +31,13 @@ public class ReflectUtil {
RelPath annotation = f.getAnnotation(RelPath.class);
String path = annotation.value();
try {
Object o = f.get(specialVoValue);
Field declaredField = reqClass.getDeclaredField(path);
Object o = f.get(requestVo);
Field declaredField = baseVoClass.getDeclaredField(path);
declaredField.setAccessible(true);
Class<?> type = declaredField.getType();
// 基本数据类型
if (isPrimate(type)) {
declaredField.set(req, o);
declaredField.set(baseVo, o);
}
// ModuleList类型
else if ("com.brilliance.mda.runtime.mda.impl.ModuleList".equals(type.getName())) {
......@@ -49,36 +50,96 @@ public class ReflectUtil {
ModuleList moduleList = new ModuleList();
for (Object data : list) {
Object dataObj = moduleT.newInstance();
String s = setObjValue(data, listT, dataObj, moduleT);
String s = setValue2RequestVo(data, listT, dataObj, moduleT);
if (!StringUtils.isBlank(s)) {
return s;
}
moduleList.add(dataObj);
}
declaredField.set(req, moduleList);
declaredField.set(baseVo, moduleList);
} else if ("java.util.List".equals(type.getName())) {
// list数据类型
declaredField.set(req, o);
declaredField.set(baseVo, o);
} else {
// 对象类型
Object typeO = type.newInstance();
String s = setObjValue(o, f.getType(), typeO, type);
String s = setValue2RequestVo(o, f.getType(), typeO, type);
if (!StringUtils.isBlank(s)) {
return s;
}
declaredField.set(req, typeO);
declaredField.set(baseVo, typeO);
}
} catch (Exception e) {
Class<?> superclass = reqClass.getSuperclass();
try {
Field superclassDeclaredField = superclass.getDeclaredField(path);
superclassDeclaredField.setAccessible(true);
superclassDeclaredField.set(req, f.get(specialVoValue));
} catch (Exception ex) {
String errMsg = specialVoClass.getName() + "中字段" + f.getName() + "反射错误";
log.error(errMsg);
return errMsg;
// Class<?> superclass = baseVoClass.getSuperclass();
// try {
// Field superclassDeclaredField = superclass.getDeclaredField(path);
// superclassDeclaredField.setAccessible(true);
// superclassDeclaredField.set(baseVo, f.get(requestVo));
// } catch (Exception ex) {
// String errMsg = requestVoClass.getName() + "中字段" + f.getName() + "反射错误";
// log.error(errMsg);
// return errMsg;
// }
String errMsg = requestVoClass.getName() + "中字段" + f.getName() + "反射错误";
log.error(errMsg, e);
return errMsg;
}
}
}
return "";
}
public static String setValue2RespondVo(Object respondVo, Class respondVoClass, Object baseVo, Class baseVoClass) {
Field[] declaredFields = respondVoClass.getDeclaredFields();
for (Field f : declaredFields) {
f.setAccessible(true);
if (f.isAnnotationPresent(RelPath.class)) {
RelPath annotation = f.getAnnotation(RelPath.class);
String path = annotation.value();
try {
Field declaredField = baseVoClass.getDeclaredField(path);
declaredField.setAccessible(true);
Object o = declaredField.get(baseVo);
if (o == null) {
continue;
}
Class<?> type = declaredField.getType();
// 基本数据类型
if (isPrimate(type)) {
f.set(respondVo, o);
} else if ("com.brilliance.mda.runtime.mda.impl.ModuleList".equals(type.getName())) {
// list数据类型
Class tType = ReflectUtil.getTType(f);
if (ReflectUtil.isPrimate(tType)) {
// list的泛型是基本类型
f.set(respondVo, o);
} else {
// 泛型是对象
List list = (List) o;
List dataList = new ArrayList(list.size());
for (Object data : list) {
Object o1 = tType.newInstance();
setValue2RespondVo(o1, tType, data, ReflectUtil.getTType(declaredField));
dataList.add(o1);
}
f.set(respondVo, dataList);
}
} else if ("java.util.List".equals(type.getName())) {
f.set(respondVo, o);
} else {
// 对象类型
Object typeO = f.getType().newInstance();
String s = setValue2RespondVo(typeO, f.getType(), o, type);
if (!StringUtils.isBlank(s)) {
return s;
}
f.set(respondVo, typeO);
}
} catch (Exception e) {
String errMsg = respondVoClass.getName() + "中字段" + f.getName() + "反射错误";
log.error(errMsg);
return errMsg;
}
}
}
......
package com.brilliance.mda.runtime.request;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class BaseVO {
protected String src;
protected String userid;
protected String id;
protected String cifno;
protected String pageId;
protected Map<String, Object> params;
protected Map<String, Object> root;
protected List<Map<String, Object>> changes;
protected int pageNum;//第几页,从1开始,比如1,2,3,4,5......
protected int pageSize;//分页每页大小
public Map<String, Object> getParams() {
return params;
}
public void setParams(Map<String, Object> parmas) {
this.params = parmas;
}
public List<Map<String, Object>> getChanges() {
return changes;
}
public void setChanges(List<Map<String, Object>> changes) {
this.changes = changes;
}
public String getPageId() {
return pageId;
}
public void setPageId(String pageId) {
this.pageId = pageId;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getSrc() {
return src;
}
/**
* 第几页,从1开始,比如1,2,3,4,5......
*/
protected int pageNum;
public void setSrc(String src) {
this.src = src;
}
/**
* 分页每页大小
*/
protected int pageSize;
public Map<String, Object> getRoot() {
return root;
}
public void setRoot(Map<String, Object> root) {
this.root = root;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment