前端字段按需返回和使用 @JsonView 返回{}

方案1 使用 @JsonView

问题---使用 @JsonView 返回{}

         原因: 如果添加了@JsonView的返回类被重新封装,这个时候这个注解不生效。

真TM恶心到了 

//JsonView 使用案例
/**
 * 发布任务信息对象 com_task_distribute_info
 *
 * @author wcl
 * @date 2022-08-25
 */
public class TaskDistributeInfo extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    public interface  TaskInfoByList {

    }
    /** 任务ID */
    private Integer id;

    /** 任务名称 */
    @Excel(name = "任务名称")
    @JsonView(TaskInfoByList.class)
    private String name;
    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

/***调用 时候返回 {} ***/
    @GetMapping("/list")
    @JsonView(TaskDistributeInfo.TaskInfoByList.class)
    public TableDataInfo list(TaskDistributeInfo taskDistributeInfo)
    {
        startPage();
        taskDistributeInfo.setCreateBy(getUsername());
        List<TaskDistributeInfo> list = 
        taskDistributeInfoService.selectTaskDistributeInfoList(taskDistributeInfo);
        return getDataTable(list);
    }


    
    /**
     * 返回正常情况写法
     */
    @GetMapping("/list")
    @JsonView(TaskDistributeInfo.TaskInfoByList.class)
    public List<TaskDistributeInfo> list(TaskDistributeInfo taskDistributeInfo)
    {
        startPage();
        taskDistributeInfo.setCreateBy(getUsername());
        List<TaskDistributeInfo> list = 
        taskDistributeInfoService.selectTaskDistributeInfoList(taskDistributeInfo);
        return list;
    }

方案二 BeanUtils.copyProperties

/**
 * 发布任务信息返回对象 
 *
 * @author wcl
 * @date 2022-09-13
 */
public class TaskDistributeInfoByList
{
    private static final long serialVersionUID = 1L;

    /** 任务ID */
    private Integer id;

    /** 任务名称 */
    @Excel(name = "任务名称")
    private String name;

    /** 任务汇报类型(1 一键发布,2 手动添加) */
    @Excel(name = "任务汇报类型", readConverterExp = "1=,一=键发布,2,手=动添加")
    private String reportType;

    /** 任务类型(1 网评,2 原创,3 语料收集) */
    @Excel(name = "任务类型", readConverterExp = "1=,网=评,2,原=创,3,语=料收集")
    private String type;

    /** 任务等级(1 紧急,2 重要,3 一般) */
    @Excel(name = "任务等级", readConverterExp = "1=,紧=急,2,重=要,3,一=般")
    private String taskLevel;

    /** 任务状态(1 待下发,2 进行中,3 已完成,4 已逾期,5 已取消) */
    @Excel(name = "任务状态", readConverterExp = "1=,待=下发,2,进=行中,3,已=完成,4,已=逾期,5,已=取消")
    private String status;

    /** 创建者 */
    private String createBy;
    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 任务开始时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "任务开始时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date startTime;

    /** 任务结束时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "任务结束时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date endTime;

    public Integer getId() {
        return this.id;
    }

    public void setId(final Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getReportType() {
        return this.reportType;
    }

    public void setReportType(final String reportType) {
        this.reportType = reportType;
    }

    public String getType() {
        return this.type;
    }

    public void setType(final String type) {
        this.type = type;
    }

    public String getTaskLevel() {
        return this.taskLevel;
    }

    public void setTaskLevel(final String taskLevel) {
        this.taskLevel = taskLevel;
    }

    public String getStatus() {
        return this.status;
    }

    public void setStatus(final String status) {
        this.status = status;
    }

    public String getCreateBy() {
        return this.createBy;
    }

    public void setCreateBy(final String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(final Date createTime) {
        this.createTime = createTime;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(final Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return this.endTime;
    }

    public void setEndTime(final Date endTime) {
        this.endTime = endTime;
    }
}
    @GetMapping("/list")
    public TableDataInfo list(TaskDistributeInfo taskDistributeInfo)
    {
        startPage();
        taskDistributeInfo.setCreateBy(getUsername());
        List<TaskDistributeInfo> list = 
        taskDistributeInfoService.selectTaskDistributeInfoList(taskDistributeInfo);
        List<TaskDistributeInfoByList> converts = TcsBeanConvertUtil.converts(list, 
        TaskDistributeInfoByList.class);
        return getDataTable(converts);
    }


 


import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class TcsBeanConvertUtil {

    /**
     * 批量copyProperties
     *
     * @param sourceList source dto's list
     * @param target     target
     * @param <T>        target CLASS
     * @return target dto's list
     */
    public static <T> List<T> converts(List<?> sourceList, Class<T> target) {
        if (CollUtil.isEmpty(sourceList)) {
            return Collections.emptyList();
        }
        return sourceList.stream().map(s -> convert(s, target)).collect(Collectors.toList());
    }

    public static <T> T convert(Object source, Class<T> t) {
        if (source == null || t == null) {
            return null;
        }
        T target;
        try {
            target = t.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        BeanUtils.copyProperties(source, target, t);
        return target;
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值