软件编程
位置:首页>> 软件编程>> java编程>> 如何只返回实体类中的部分字段问题

如何只返回实体类中的部分字段问题

作者:来齐  发布时间:2022-11-28 05:09:53 

标签:返回,实体类,部分字段

如何只返回实体类中的部分字段

在实体类上添加注解

@JsonInclude(JsonInclude.Include.NON_EMPTY)

表示实体类中为null,空字符串,空集合数组等内容不会被序列化,即不会返回字段和值。

以下为对注解所有取值的简介

ALWAYS // 默认策略,任何情况都执行序列化
NON_NULL // 非空
NON_ABSENT // null的不会序列化,但如果类型是AtomicReference,依然会被序列化
NON_EMPTY // null、集合数组等没有内容、空字符串等,都不会被序列化
NON_DEFAULT // 如果字段是默认值,就不会被序列化
CUSTOM // 此时要指定valueFilter属性,该属性对应一个类,用来自定义判断被JsonInclude修饰的字段是否序列化
USE_DEFAULTS // 当JsonInclude在类和属性上都有时,优先使用属性上的注解,此时如果在序列化的get方法上使用了JsonInclude,并设置为USE_DEFAULTS,就会使用类注解的设置

java动态添加实体类字段并返回给前端

工具类

package com.bless.wms.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
/**
* 动态添加实体类字段
*/
@Slf4j
public final class PropertyAppender {
   private static final class DynamicBean {
       private Object target;
       private BeanMap beanMap;
       private DynamicBean(Class superclass, Map<String, Class> propertyMap) {
           this.target = generateBean(superclass, propertyMap);
           this.beanMap = BeanMap.create(this.target);
       }
       private void setValue(String property, Object value) {
           beanMap.put(property, value);
       }
       private Object getValue(String property) {
           return beanMap.get(property);
       }
       private Object getTarget() {
           return this.target;
       }
       /**
        * 根据属性生成对象
        */
       private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
           BeanGenerator generator = new BeanGenerator();
           if (null != superclass) {
               generator.setSuperclass(superclass);
           }
           BeanGenerator.addProperties(generator, propertyMap);
           return generator.create();
       }
   }
   public static Object generate(Object dest, Map<String, Object> newValueMap) throws InvocationTargetException, IllegalAccessException {
       PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
       //1.获取原对象的字段数组
       PropertyDescriptor[] descriptorArr = propertyUtilsBean.getPropertyDescriptors(dest);
       //2.遍历原对象的字段数组,并将其封装到Map
       Map<String, Class> oldKeyMap = new HashMap<>();
       for (PropertyDescriptor it : descriptorArr) {
           if (!"class".equalsIgnoreCase(it.getName())) {
               oldKeyMap.put(it.getName(), it.getPropertyType());
               newValueMap.put(it.getName(), it.getReadMethod().invoke(dest));
           }
       }
       //3.将扩展字段Map合并到原字段Map中
       newValueMap.forEach((k, v) -> oldKeyMap.put(k, v.getClass()));
       //4.根据新的字段组合生成子类对象
       DynamicBean dynamicBean = new DynamicBean(dest.getClass(), oldKeyMap);
       //5.放回合并后的属性集合
       newValueMap.forEach((k, v) -> {
           try {
               dynamicBean.setValue(k, v);
           } catch (Exception e) {
               log.error("动态添加字段【值】出错", e);
           }
       });
       return dynamicBean.getTarget();
   }
}

调用

如何只返回实体类中的部分字段问题

前端接口调用测试

如何只返回实体类中的部分字段问题

备注:前端表格直接for循环动态渲染就OK了

来源:https://blog.csdn.net/m0_46528706/article/details/111762731

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com