Java 通过反射获取注解属性信息
一、创建存储对象
// 属性对象存储类 public class MetadataFiled { /** * key 对应对象中间的属性 */ private String key; /** * 字段标签 */ private String label; public MetadataFiled(String key, String label) { this.key = key; this.label = label; } public String getKey() { return key; } public String getLabel() { return label; } public void setKey(String key) { this.key = key; } public void setLabel(String label) { this.label = label; } }
二、工具类(通过ApiModelProperty注解获取字段属性信息)
// 对象属性封装类 public class ReflectionUtils { /** * 获取属性名(使用下划线命名法)与对应中文名的映射关系。 * * @param clazz 要获取属性信息的类 * @return 属性名与中文名的映射关系 */ public static Map<String, String> getFiledMap(Class<?> clazz) { Map<String, String> propertyMap = new HashMap<>(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); if (annotation != null) { String propertyName = field.getName(); String chineseName = annotation.value(); // 将驼峰命名转换为下划线命名 String underscoredName = camelToUnderscore(propertyName); propertyMap.put(underscoredName, chineseName); } } return propertyMap; } /** * 获取属性元数据列表,每个元素包含属性名(使用下划线命名法)和对应中文名。 * * @param clazz 要获取属性信息的类 * @return 包含属性元数据的列表 */ public static List<MetadataFiled> getFiled(Class<?> clazz) { List<MetadataFiled> propertyMetadataList = new ArrayList<>(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class); if (annotation != null) { String propertyName = field.getName(); String chineseName = annotation.value(); // 将驼峰命名转换为下划线命名 String underscoredName = camelToUnderscore(propertyName); propertyMetadataList.add(new MetadataFiled(underscoredName, chineseName)); } } return propertyMetadataList; } /** * 将驼峰命名字符串转换为下划线命名。 * * @param input 驼峰命名的字符串 * @return 转换为下划线命名的字符串 */ private static String camelToUnderscore(String input) { StringBuilder result = new StringBuilder(); for (int i = 0; i < input.length(); i++) { char currentChar = input.charAt(i); if (Character.isUpperCase(currentChar)) { result.append("_").append(Character.toLowerCase(currentChar)); } else { result.append(currentChar); } } return result.toString(); } }
三、构建 ApiModelProperty注解对象
import com.fasterxml.jackson.annotation.JsonFormat; import com.yf.mybatis.entity.BaseEntity; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; /** * @author : FengQing * @date :Created in 2023-11-10 * @modified By: * @version: 1.0 */ @Data @NoArgsConstructor @AllArgsConstructor public class ReceiptDTO extends BaseEntity { private static final long serialVersionUID = 1L; @ApiModelProperty("测试1") private String channelName; @ApiModelProperty("测试2") private String channelAs; }
四、前端控制器调用
@ApiOperation(value = "分页列表", notes = "分页列表") @PostMapping("/page") public R<PageUtils<ReceiptVo>> list(@RequestBody Map<String, Object> params){ PageUtils<ReceiptVo> page = receiptService.selectReceiptPage(params); // 获取属性名和中文描述的映射 page.setFields(ReflectionUtils.getField(ReceiptDTO.class)); return R.data(page); }
前端返回结果:【fields】获取属性名和中文描述的映射
{ "code": 0, "msg": "操作成功", "data": { "totalCount": 1, "size": 10, "totalPage": 1, "current": 1, "fields": [ { "key": "channel_name", "label": "测试1" }, { "key": "channel_as", "label": "测试2" } ], "list": [] } }