Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問題
反射機(jī)制數(shù)據(jù)傳值為空的問題
兩個(gè)小方法,用于解決BeanUtils.copyProperties(x, y);中源對象的值為空問題
1.通過實(shí)體注解數(shù)據(jù)庫字段為Map的Key,需要的非空值為Value封裝數(shù)據(jù)
@Override ? ? public Map<String, Object> setNodeParamItems(DispatchInfoItem dispatchInfoItem) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { ? ? ? ? Map<String, Object> map = new HashMap<>(); ? ? ? ? DispatchInfo dispatchInfo = new DispatchInfo(); ? ? ? ? if (null != dispatchInfoItem) { ? ? ? ? ? ? BeanUtils.copyProperties(dispatchInfoItem, dispatchInfo); ? ? ? ? } ? ? ? ? Method[] methods = dispatchInfo.getClass().getDeclaredMethods(); ? ? ? ? if (methods != null) { ? ? ? ? ? ? for (Method method : methods) { ? ? ? ? ? ? ? ? String methodName = method.getName(); ? ? ? ? ? ? ? ? if (methodName.startsWith("get")) { ? ? ? ? ? ? ? ? ? ? Column column = dispatchInfo.getClass().getDeclaredMethod(methodName).getAnnotation(Column.class); ? ? ? ? ? ? ? ? ? ? Object value = method.invoke(dispatchInfo); ? ? ? ? ? ? ? ? ? ? if (null != column && StringUtils.isNotBlank(StringHelper.getString(value))) { ? ? ? ? ? ? ? ? ? ? ? ? map.put(column.name(), value); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return map; ? ? }
2.根據(jù)獲取的值注入;
public void getMethods(DispatchInfo dispatchInfo, Map<String, Object> map) throws Exception { ? ? ? ? //獲取方法上的注解值 ? ? ? ? Method[] methods = dispatchInfo.getClass().getDeclaredMethods(); ? ? ? ? if (methods != null) { ? ? ? ? ? ? for (Method method : methods) { ? ? ? ? ? ? ? ? String methodName = method.getName(); ? ? ? ? ? ? ? ? if (methodName.startsWith("get")) { ? ? ? ? ? ? ? ? ? ? Column column = dispatchInfo.getClass().getDeclaredMethod(methodName).getAnnotation(Column.class); ? ? ? ? ? ? ? ? ? ? if (column != null) { ? ? ? ? ? ? ? ? ? ? ? ? String setMethodName = methodName.replaceFirst("(get)", "set"); ? ? ? ? ? ? ? ? ? ? ? ? Method setMethod = dispatchInfo.getClass().getMethod(setMethodName, method.getReturnType()); ? ? ? ? ? ? ? ? ? ? ? ? ; ? ? ? ? ? ? ? ? ? ? ? ? if (null != map.get(column.name())) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? setMethod.invoke(dispatchInfo, map.get(column.name())); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? }
3.根據(jù)值進(jìn)行實(shí)際的操作
java 反射 處理 空值
package org.zkdg.utils.spring.annotations.impl; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.sql.SQLException; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.zkdg.utils.entity.AjaxEntity; import org.zkdg.utils.spring.annotations.NNull; @Aspect @Component /** * * @author 王海明 * @createData 2017年7月13日 上午8:36:23 * @說明 :出了一些空值。。。 */ public class AjaxEntityHandler { // @Pointcut("@annotation(org.zkdg.utils.annotations.AfterHandler)") @Pointcut("@annotation(org.zkdg.utils.spring.annotations.NullValidate)") // @Pointcut("execution(* org.dcexam.*.service.*.*(..))") public void beforeCall() { // service方法調(diào)用之前,檢測參數(shù),僅限第一個(gè)參數(shù), 不能為空值 } /** * service發(fā)生異常時(shí)調(diào)用 */ @Pointcut("execution(* org.dcexam.*.service.*.*(..))") public void afterThrowEx() { System.out.println("************\n\n\n\n\n\n\n\n\n\n\n\n*******"); } @Around(value = "beforeCall()") public AjaxEntity doBefore(ProceedingJoinPoint point) throws Throwable { // TODO Auto-generated method stub // 判斷不能為空 Object[] args = point.getArgs(); if (args == null || args[0] == null) { return new AjaxEntity("warning", "未選擇任何數(shù)據(jù)。。。"); } // 獲取代理對象類方法參數(shù) MethodSignature target = (MethodSignature) point.getSignature(); Annotation[][] annotations = target.getMethod().getParameterAnnotations(); int argsIndex = 0; StringBuilder sb = new StringBuilder(); for (Annotation[] annotation : annotations) { NNull nn = (NNull) annotation[0]; String[] descs = nn.desc(); String[] fields = nn.field(); if (fields.length > 0 && fields.length > 0 && descs.length == fields.length) { for (int i = 0; i < fields.length; i++) { Field field = args[argsIndex].getClass().getDeclaredField(fields[i]); // 允許訪問 field.setAccessible(true); Object object = field.get(args[argsIndex]); if (object == null) { sb.append(descs[i]).append("不能為空。。。<br>"); } if (object instanceof String) { String string = (String) object; if (string.trim().length() == 0) sb.append(descs[i]).append("不能為空。。。<br>"); else if (string.trim().equals("0")) sb.append("未選擇" + descs[i] + "。。。<br>"); } else if (object instanceof Number) { Integer integer = (Integer) object; if (integer == 0) sb.append("未選擇" + descs[i] + "。。。<br>"); } } if (sb.length() > 0) return AjaxEntity.ERROR(sb.toString()); } argsIndex++; } // 加上@Nullvalidate 注解,不允許出現(xiàn)空 值 for (Object obj : args) { if (obj == null) { return AjaxEntity.WARNING("出現(xiàn)了不允許的空值"); } else if (obj instanceof String) { if (((String) obj).length() == 0) { return AjaxEntity.WARNING("出現(xiàn)了不允許的空值"); } } } AjaxEntity ajax = (AjaxEntity) point.proceed(args); return ajax == null ? AjaxEntity.ERROR("操作失敗") : ajax; } /** * * @param joinPoint * 連接點(diǎn) * @param ex * 異常 * @return AjaxEntity 異常信息 */ @AfterThrowing(value = "afterThrowEx()", throwing = "ex") public void doAfterThrowEx(JoinPoint joinPoint, Exception ex) { AjaxEntity ajax = new AjaxEntity(); if (ex.getCause() instanceof SQLException) { // 數(shù)據(jù)庫操作異常 ajax = AjaxEntity.ERROR("操作數(shù)據(jù)庫時(shí)出現(xiàn)異常"); } } }
另外,java 命名時(shí) 。屬性最好不要有下劃線,否則可能會(huì)粗錯(cuò)。。。。。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis plus saveBatch方法方法執(zhí)行慢導(dǎo)致接口發(fā)送慢解決分析
這篇文章主要為大家介紹了mybatis plus saveBatch方法導(dǎo)致接口發(fā)送慢解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10SpringBoot利用validation實(shí)現(xiàn)優(yōu)雅的校驗(yàn)參數(shù)
數(shù)據(jù)的校驗(yàn)是交互式網(wǎng)站一個(gè)不可或缺的功能,如果數(shù)據(jù)庫中出現(xiàn)一個(gè)非法的郵箱格式,會(huì)讓運(yùn)維人員頭疼不已。本文將介紹如何利用validation來對數(shù)據(jù)進(jìn)行校驗(yàn),感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-06-06Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決場景分析
當(dāng)運(yùn)行一個(gè)Spring Boot項(xiàng)目時(shí),如果未設(shè)置JVM內(nèi)存參數(shù),Spring Boot默認(rèn)會(huì)采用JVM自身默認(rèn)的配置策略,接下來通過本文給大家介紹Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決方法,需要的朋友參考下吧2021-08-08mybatis多表查詢的實(shí)現(xiàn)(xml方式)
本文主要介紹了mybatis多表查詢的實(shí)現(xiàn)(xml方式),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03實(shí)戰(zhàn)指南:Java編寫Flink?SQL解決難題
想知道如何利用Java編寫Flink?SQL解決難題嗎?本指南將為您揭示最實(shí)用的技巧和策略,讓您輕松應(yīng)對挑戰(zhàn),跟著我們一起探索,讓Java和Flink?SQL成為您問題解決的得力助手!2023-12-12java網(wǎng)絡(luò)編程基礎(chǔ)知識介紹
這篇文章主要介紹了java網(wǎng)絡(luò)編程基礎(chǔ)知識介紹,涉及OSI分層模型和TCP/IP分層模型的對應(yīng)關(guān)系、IP地址、端口號、tcp、udp等相關(guān)內(nèi)容,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-11-11Java實(shí)現(xiàn)有限狀態(tài)機(jī)的推薦方案分享
有限狀態(tài)機(jī)又稱有限狀態(tài)自動(dòng)機(jī),簡稱狀態(tài)機(jī),是表示有限個(gè)狀態(tài)以及在這些狀態(tài)之間的轉(zhuǎn)移和動(dòng)作等行為的數(shù)學(xué)模型,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)有限狀態(tài)機(jī)的推薦方案,需要的朋友可以參考下2021-11-11Docker搭建前端Java的開發(fā)環(huán)境詳解
相信每個(gè)人入職第一天就是搭建本地開發(fā)環(huán)境,因?yàn)槲宜居玫氖莏ava,看見了多年不見的eclipse的圖標(biāo)出現(xiàn)我的電腦上,我是難過的。后來知道并不是我一個(gè)人有此感受。這篇文章是為了解決前后端開發(fā)沒有徹底分離的坑,詳細(xì)的給大家介紹了利用Docker搭建前端Java的開發(fā)環(huán)境。2016-10-10