@JsonView是jackson json中的一个注解,spring webmvc也支持这个注解。

这个注解的作用就是控制输入输出后的json.

假设我们有一个用户类,其中包含用户名和密码,一般情况下如果我们需要序列化用户类时,密码也会被序列化,在一般情况下我们肯定不想见到这样的情况。但是也有一些情况我们需要把密码序列化,如何解决这两种不同的情况呢?

使用@JsonView就可以解决。

看下面的简单例子:

public class User {
public interface WithoutPasswordView {};
public interface WithPasswordView extends WithoutPasswordView {}; private String username;
private String password; public User() {
} public User(String username, String password) {
this.username = username;
this.password = password;
} @JsonView(WithoutPasswordView.class)
public String getUsername() {
return this.username;
} @JsonView(WithPasswordView.class)
public String getPassword() {
return this.password;
}
}

有这样一个简单的User对象,包含两个简单的属性。

在这个对象中定义了两个接口,其中WithoutPasswordView指的就是不带密码的视图,WithPasswordView指的是带密码的视图,并且继承了WithoutPasswordView的视图。

@JsonView 中的这个视图不仅可以用接口,也可以是一般的类,或者说只要有Class属性就能当成视图使用。

类或接口间的继承,也是视图之间的继承,继承后的视图会包含上级视图注解的方法。

@JsonView 可以写到方法上或者字段上。

下面通过代码测试上面的User对象:

public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
//创建对象
User user = new User("isea533","123456");
//序列化
ByteArrayOutputStream bos = new ByteArrayOutputStream();
objectMapper.writerWithView(User.WithoutPasswordView.class).writeValue(bos, user);
System.out.println(bos.toString()); bos.reset();
objectMapper.writerWithView(User.WithPasswordView.class).writeValue(bos, user);
System.out.println(bos.toString());
}

先创建一个objectMapper,然后通过writerWithView工厂方法创建一个指定视图的ObjectWritter,然后通过writeValue输出结果。

输出结果:

{"username":"isea533"}
{"username":"isea533","password":"123456"}

@JsonView 使用起来就是这么简单,没有太复杂的东西。

@JsonView 属性可以写在注解上,这点不知道是否类似Spring中的自定义注解,如果有了解的人可以留言,谢谢。

另外在Spring-webmvc中使用@JsonView 时要注意,虽然该注解允许指定多个视图,但是spring-webmvc只支持一个参数(视图)。

spring boot 之@JsonView 简单介绍的更多相关文章

  1. Spring Boot 配置_yaml语法介绍 day02

    一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...

  2. Spring history&Design Philosophy 简单介绍~

    SPRING框架的介绍和历史 Spring Framework是一个开源Java应用程序框架,最初是基于依赖注入(DI)和控制反转(IoC)的原理开发的. Spring Framework已经成长为控 ...

  3. 使用idea搭建Spring boot+jsp的简单web项目

    大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8   idea2017 ...

  4. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  5. Spring Boot 系列 - WebSocket 简单使用

    在实现消息推送的项目中往往需要WebSocket,以下简单讲解在Spring boot 中使用 WebSocket. 1.pom.xml 中引入 spring-boot-starter-websock ...

  6. Spring boot(四)thymeleaf使用介绍

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  7. spring boot应用测试框架介绍

    一.spring boot应用测试存在的问题 官方提供的测试框架spring-boot-test-starter,虽然提供了很多功能(junit.spring test.assertj.hamcres ...

  8. 关于spring boot上手的一点介绍

    在spring官网网址 https://spring.io/guides 下,有许多相关介绍,包括可以构建的例子程序. 使用intellij idea,可以通过新建 spring boot initi ...

  9. 对Spring Boot 及Mybatis简单应用

    因为没有系统的学习过SpringBoot,在对照一个别人的SpringBoot项目,进行简单的搭建及使用. 1.首先创建SpringBoot项目之后,这里会有默认的启动类,基本不需要配置,在类的上边有 ...

随机推荐

  1. WebStrom配置node.js

    Webstrom的注册码: WebStorm 7.0.1注册码 user name:newasp 注册码: ===== LICENSE BEGIN ===== 16417-12042010 00001 ...

  2. ***PHP $_FILES函数详解 + PHP文件上传 move_uploaded_file() 参数的正确写法

    PHP $_FILES函数详解 在PHP中上传一个文件建一个表单要比ASP中灵活得多.具体的看代码. 如:  复制代码代码如下: <form enctype="multipart/fo ...

  3. (转载)How browsers work--Behind the scenes of modern web browsers (前端必读)

    浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器— ...

  4. Python实现截图

    本文主要介绍了Python实现截图的两种方式,使用PIL的方法和不使用PIL的方法.文中也涉及到了一些位图的知识.

  5. Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】

    No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...

  6. SVN同步时报错:“Previous operation has not finished; run 'cleanup' if it was interrupted”

    SVN同步时报错:“Previous operation has not finished; run 'cleanup' if it was interrupted” 这大概是SVN之前的操作没有完成 ...

  7. Win10 主题 美化 动漫

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com High School D×D 塔城白音Win7主题+Win8主题+Win10主题 Win10 ...

  8. 【UOJ#179】线性规划 单纯形

    题目链接: http://uoj.ac/problem/179 Solution 就是单纯形模板题,这篇博客就是存一下板子. Code #include<iostream> #includ ...

  9. HDU 5840 This world need more Zhu 树链剖分+暴力

    This world need more Zhu 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5840 Description As we all ...

  10. c# RSA 加密解密 java.net公钥私钥转换 要解密的模块大于128字节

    有一个和接口对接的任务,对方使用的是java,我方使用的是c#,接口加密类型为RSA,公钥加密私钥解密. 然后就是解决各种问题. 1.转换对方的密钥字符串 由于c#里面需要使用的是xml各式的密钥字符 ...