操作针对的是在前后端交互过程中信息和数据传递的问题。在前后端的交互过程中,从后台获取数据一直是交互的核心需求之一。通常情况下,我们习惯于将返回的数据按照json的格式解析成为字符串,然后将这个字符串作为真正的数据返回。由于spring提供的responseBody注解,可以使方法返回的对象默认处理为Json的格式。在ResponseBody基础上,spring还提供了集ResposeBody和controller于一体的RestController,表示这整个controller中的方法返回值都按照responseBody的方式处理。这使得在编写接口时可以直接将需要的数据以对象的形式返回,这大大减少了繁琐的操作,提高了开发效率。
实体类:
public class Alex {
private Map<String, Object> message = new HashMap();
private Map<String, Object> executiveUser = new HashMap();
private Map<String, Object> parameter = new HashMap();
private Map<String, Object> result = new HashMap();
public Alex() {
}
public Map<String, Object> getMessage() {
return this.message;
}
public void setMessage(Map<String, Object> message) {
this.message = message;
}
public Map<String, Object> getExecutiveUser() {
return this.executiveUser;
}
public void setExecutiveUser(Map<String, Object> executiveUser) {
this.executiveUser = executiveUser;
}
public Map<String, Object> getParameter() {
return this.parameter;
}
public void setParameter(Map<String, Object> parameter) {
this.parameter = parameter;
}
public Map<String, Object> getResult() {
return this.result;
}
public void setResult(Map<String, Object> result) {
this.result = result;
}
public Object getMessageKey(String key) {
return this.message.get(key);
}
public void setMessageValue(String key, Object value) {
this.message.put(key, value);
}
public Object getExecutiveUserKey(String key) {
return this.executiveUser.get(key);
}
public void setExecutiveUserValue(String key, Object value) {
this.executiveUser.put(key, value);
}
public Object getParameterKey(String key) {
return this.parameter.get(key);
}
public void setParameterValue(String key, Object value) {
this.parameter.put(key, value);
}
public Object getResultKey(String key) {
return this.result.get(key);
}
public void setResultValue(String key, Object value) {
this.result.put(key, value);
}
public <T> List<T> getParameterCellList(String key, Class<T> clazz) {
List<T> list = new ArrayList();
List<Map> map = (List)this.getParameterKey(key);
Iterator var5 = map.iterator();
while(var5.hasNext()) {
Map m = (Map)var5.next();
try {
list.add(AlexUtils.mapChangeObject(m, clazz));
} catch (Exception var8) {
var8.printStackTrace();
}
}
return list;
}
}
实体类:
public class AlexCustom {
public static final int STATUS_SUCCESS = 1;
public static final int STATUS_DEFAULT = 0;
public static final int STATUS_FAILURE = 2;
public static String fileName = "fileName";
public static String methodName = "methodName";
public static String status = "status";
public static String msg = "msg";
public AlexCustom() {
}
}
工具类:
public class AlexUtils {
public AlexUtils() {
}
public static <T> T mapChangeObject(Map map, Class<T> type) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
T obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
PropertyDescriptor[] var5 = propertyDescriptors;
int var6 = propertyDescriptors.length;
for(int var7 = 0; var7 < var6; ++var7) {
PropertyDescriptor pro = var5[var7];
if (map.containsKey(pro.getName())) {
pro.getWriteMethod().invoke(obj, map.get(pro.getName()));
}
}
return obj;
}
}
返回状态类:
public class ResultStatus {
public ResultStatus() {
}
public static Alex ResultSuccessAndStatus(Alex alex, String msg) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
alex.setMessageValue(AlexCustom.fileName, stackTraceElement.getFileName());
alex.setMessageValue(AlexCustom.methodName, stackTraceElement.getMethodName());
alex.setMessageValue(AlexCustom.status, 1);
alex.setMessageValue(AlexCustom.msg, msg);
return alex;
}
public static Alex ResultErrorMsg(Alex alex, String msg) {
StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
alex.setMessageValue(AlexCustom.fileName, stackTraceElement.getFileName());
alex.setMessageValue(AlexCustom.methodName, stackTraceElement.getMethodName());
alex.setMessageValue(AlexCustom.status, 2);
alex.setMessageValue(AlexCustom.msg, msg);
return alex;
}
}
工具类:
public class TimeUtils {
public TimeUtils() {
}
public static String getUUID16() {
String uuid32 = UUID.randomUUID().toString().replace("-", "");
String uuid16 = uuid32.substring(0, 16);
return uuid16;
}
public static String getUUID8() {
String uuid32 = UUID.randomUUID().toString().replace("-", "");
String uuid16 = uuid32.substring(0, 8);
return uuid16;
}
public static synchronized Long getUUIDTOLong() {
try {
Thread.sleep(1L);
} catch (InterruptedException var3) {
var3.printStackTrace();
}
String msg = "";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
msg = sdf.format(date).substring(2);
return Long.parseLong(msg);
}
public static String getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
}
其他项目引入依赖:
<dependency>
<groupId>com.hope.cloud</groupId> <!--自定义-->
<artifactId>cloud</artifactId> <!--自定义-->
<version>1.0</version> <!--自定义-->
<scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
<systemPath>${basedir}/lib/alex-6.6.6.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
<build>
<finalName>demo666</finalName><!--更改包名-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
controller层前后端交互测试:
//查询所有任务的信息
@RequestMapping(value = "/getTableConsumeOpcUaValueList", method = RequestMethod.POST)
@ApiOperation(value = "查询opc设备当前的值的集合数据", notes = "返回所有的数据 list")
@ApiImplicitParam(required = false, paramType = "body", name = "", value = "所有数据", dataType = "alex")
public Alex getTableConsumeOpcUaValueList(@RequestBody Alex alex) {
Map<Object, Object> map = new HashMap<>();
//区域
String deviceRegion = (String) alex.getParameterKey("deviceRegion");
//名称
String consumeAbridge = (String) alex.getParameterKey("consumeAbridge");//缩写
String consumePosition = (String) alex.getParameterKey("consumePosition");//点位
int targetPage = (int) alex.getParameterKey("currentPage");
//每页条数
int pageSize = (int) alex.getParameterKey("currentSize");
map.put("deviceRegion", deviceRegion);
map.put("consumeAbridge", consumeAbridge);
map.put("pageSize", pageSize);
map.put("targetPage", targetPage);
map.put("consumePosition", consumePosition);
Map<Object, Object> tableConsumeList = tableConsumeMapperService.getTableConsumeListMap(map);
alex.setResultValue("list", tableConsumeList);
return alex;
}