SLK的个人博客

  • 首页

  • 搜索
LRU Stack ThreadLocal Nacos RejectedExecutionHandler Executor RocketMQ ConcurrentHashMap CyclicBarrier Semaphore CountDownLatch canal Unsafe Atomic BlockingQueue AQS ReentrantLock Synchronized MESI Volatile JMM BufferPool Explain MySQL 常量池 Arthas JVM调优 三色标记 CMS ParNew 垃圾收集器 G1 Java Redis Android HTTPDNS DNS ioc 爬虫 seleniumhq 推荐引擎 Mahout IM Netty Vert.x HashMap 梯子 翻墙 V2ray Docker 搜索引擎 SpringBoot elasticsearch

SpringBoot result风格响应指定字段

发表于 2018-08-23 | 分类于 Java | 0 | 阅读次数 284

前言

在SpringBoot开发过程中 总会遇到 并不需要将Bean对象中的所有字段全部响应

这时候 我们就可以使用到JsonView这个注解了

编写接口

其实这里的接口 只是用来做标记使用的 给Bean的字段进行分类标记

在JSONViewInterface下Video这个接口是我拿来分类的 Video下有一个ViewHot 这个ViewHot继承Base

而Video下的All继承了ViewHot 后面我们看下效果

记住这个Base 要让JSONViewInterface里面所有的接口去继承它 (分类接口除外)

public interface JSONViewInterface {
    interface Video {
         
        interface ViewHot extends Base {
    
        }
        interface All extends ViewHot {

        }
    }
    interface Base {
    
    } 
}

编写Entity

我们先来看Result的Entity
Result风格的API 主要是统一响应的风格 我这里举个例子
重点看vdata 这个字段 这个字段呢 是给客户端响应的数据

我给它加了个注解是 @JsonView(JSONViewInterface.Base.class)

public class ResponeEntity implements Serializable {

    @JsonView(JSONViewInterface.Base.class)
    public int errorid;

    @JsonView(JSONViewInterface.Base.class)
    public String errordesc;
        
    @JsonView(JSONViewInterface.Base.class)
    public Object vdata;
    
}

接下来我们再看需要响应的实体类
可以看到 vtMid被我注解了 @JsonView(JSONViewInterface.Video.ViewHot.class)
vtId 则注解了 @JsonView(JSONViewInterface.Video.All.class)

public class VTopTableModel {
    @JsonView(JSONViewInterface.Video.ViewHot.class)
    private Long vtMid;

    @JsonView(JSONViewInterface.Video.All.class)
    private Long vtId;
}

编写Controller

接下来我们看Controller控制器

@JsonView(JSONViewInterface.Video.ViewHot.class)
这个注解表示要响应标记JSONViewInterface.Video.ViewHot.class这个接口的字段

@JsonView(JSONViewInterface.Video.ViewHot.class)
@RequestMapping("/hotVideo")
public ResponeEntity hotVideo() {
    List vTopTableModels = vTopService.selectHomeHot(getPageNum(), getPageSize());
    ResponeEntity responeEntity = new ResponeEntity();
    responeEntity.setErrorid(0);
    responeEntity.setErrordesc("操作成功");
    responeEntity.setVdata(vTopTableModels);
    return responeEntity;
}

最后看一下响应数据

ps: 在Controller 注解了JSONViewInterface.Video.ViewHot.class 所以只响应标记了这个接口的字段

    {
        "errorid": 0,
        "errordesc": "操作成功",
        "vdata": [
            {
                "vtMid": "928fdfaef68e42e6803bc0fc9e04cc7a"
            },
            {
                "vtMid": "0fa8934d5cd44f63b011d392ecba78ed"
            },
            {
                "vtMid": "060f36725ac14b1596955c589e0b96a8"
            },
            {
                "vtMid": "70c845fbb0294d28bbd6e31090467fbb"
            },
            {
                "vtMid": "a12548ecfc084712848937f961402fb3"
            },
            {
                "vtMid": "444336fe669b475bb8d33a682aeb27f6"
            },
            {
                "vtMid": "8b6fdc1c51a641928c0cb69c740b0310"
            },
            {
                "vtMid": "a65c668e97c748c184dddd8eb401129c"
            },
            {
                "vtMid": "56e79c6ce068469db6ebe2ac359f2cba"
            },
            {
                "vtMid": "c5f63cf5933a4db08f68829e1aa789ec"
            }
        ]
    }

再往下看标记 JSONViewInterface.Video.All.class
Controller 修改标记接口

@JsonView(JSONViewInterface.Video.All.class)
@RequestMapping("/hotVideo")
public ResponeEntity hotVideo() {
    List vTopTableModels = vTopService.selectHomeHot(getPageNum(), getPageSize());
    ResponeEntity responeEntity = new ResponeEntity();
    responeEntity.setErrorid(0);
    responeEntity.setErrordesc("操作成功");
    responeEntity.setVdata(vTopTableModels);
    return responeEntity;
}

响应数据

    {
        "errorid": 0,
        "errordesc": "操作成功",
        "vdata": [
            {
                "vtMid": "928fdfaef68e42e6803bc0fc9e04cc7a",
                "vtId": 0
            },
            {
                "vtMid": "0fa8934d5cd44f63b011d392ecba78ed",
                "vtId": 1
            },
            {
                "vtMid": "060f36725ac14b1596955c589e0b96a8",
                "vtId": 2
            },
            {
                "vtMid": "70c845fbb0294d28bbd6e31090467fbb",
                "vtId": 3
            },
            {
                "vtMid": "a12548ecfc084712848937f961402fb3",
                "vtId": 4
            },
            {
                "vtMid": "444336fe669b475bb8d33a682aeb27f6",
                "vtId": 5
            },
            {
                "vtMid": "8b6fdc1c51a641928c0cb69c740b0310",
                "vtId": 6
            },
            {
                "vtMid": "a65c668e97c748c184dddd8eb401129c",
                "vtId": 7
            },
            {
                "vtMid": "56e79c6ce068469db6ebe2ac359f2cba",
                "vtId": 8
            },
            {
                "vtMid": "c5f63cf5933a4db08f68829e1aa789ec",
                "vtId": 9
            }
        ]
    }

结尾 因为JSONViewInterface.Video.All接口继承了 JSONViewInterface.Video.ViewHot

所以 在Controller标记了 All接口 会将标记了ALL父类的字段也响应出来

# LRU # Stack # ThreadLocal # Nacos # RejectedExecutionHandler # Executor # RocketMQ # ConcurrentHashMap # CyclicBarrier # Semaphore # CountDownLatch # canal # Unsafe # Atomic # BlockingQueue # AQS # ReentrantLock # Synchronized # MESI # Volatile # JMM # BufferPool # Explain # MySQL # 常量池 # Arthas # JVM调优 # 三色标记 # CMS # ParNew # 垃圾收集器 # G1 # Java # Redis # Android # HTTPDNS # DNS # ioc # 爬虫 # seleniumhq # 推荐引擎 # Mahout # IM # Netty # Vert.x # HashMap # 梯子 # 翻墙 # V2ray # Docker # 搜索引擎 # SpringBoot # elasticsearch
Mahout Java推荐引擎
SpringBoot 不依赖注入获取application.yml参数
  • 文章目录
  • 站点概览
宋龙宽

宋龙宽

87 日志
13 分类
53 标签
RSS
Github E-mail
Creative Commons
Links
  • 黑客派
  • Relyn
  • 张小妞的博客
  • ElasticSearch教程
© 2021 宋龙宽
由 Halo 强力驱动
|
主题 - NexT.Gemini v5.1.4