1、User类

package com.imooc.model;

import com.fasterxml.jackson.annotation.JsonView;

/**
* @author oy
* @date 2019年6月22日 下午10:42:03
* @version 1.0.0
*/
public class User {
public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {}; private String username;
private String password; @JsonView(UserSimpleView.class)
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @JsonView(UserDetailView.class)
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

2、IndexController

package com.imooc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.model.User;
import com.imooc.model.User.UserSimpleView;
import com.imooc.service.UserService; /**
* @author oy
* @date 2019年6月22日 下午10:17:37
* @version 1.0.0
*/
@RestController
public class IndexController {
@Autowired
private UserService userService; @JsonView(UserSimpleView.class)
@GetMapping("/user")
public List<User> getUsers() {
return userService.getUsers();
}
}

3、测试用例

package com.imooc.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; /**
* @author oy
* @date 2019年6月22日 下午11:14:56
* @version 1.0.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class IndexControllerTest { @Autowired
private WebApplicationContext wac; private MockMvc mocMvc; @Before
public void setUp() throws Exception {
mocMvc = MockMvcBuilders.webAppContextSetup(wac).build();
} @Test
public void testGetUsers() throws Exception {
String mvcResult = mocMvc.perform(get("/user")
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
.andDo(print()) // 打印信息
.andExpect(status().isOk()) // 期望返回的状态码为200
.andReturn().getResponse().getContentAsString(); System.out.println("mvcResult: " + mvcResult);
//mvcResult: [{"username":"张三","password":"123"},{"username":"李四","password":"123"}]
//@JsonView(UserSimpleView.class) ==> mvcResult: [{"username":"张三"},{"username":"李四"}]
}
}

  

  当IndexController#getUsers()方法加上注解@JsonView(UserSimpleView.class)后,测试打印结果:mvcResult: [{"username":"张三"},{"username":"李四"}],

  当IndexController#getUsers()方法加上注解@JsonView(UserDetailView.class)后,测试打印结果:mvcResult: [{"username":"张三","password":"123"},{"username":"李四","password":"123"}]

@JsonView注解指定返回的model类中显示的字段的更多相关文章

  1. 项目实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错,eclipse中配置lombok

    @Data注解来源与Lombok,可以减少代码中大量的set get方法,大量减少冗余代码,但是今天部署项目时候,发现实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错. ...

  2. 使用@JsonView注解控制返回的Json属性

    我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高.还是记录以下,万一今后用到了呢 ⒈在实体类中使用接口来声明该实体类的多个视图. ⒉在实体类的属性get方 ...

  3. KVC在定义Model类中的妙用

    @我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ...

  4. Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效

    @Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...

  5. django的Model 模型中常用的字段类型

    常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...

  6. spring 项目中在类中注入静态字段

    有时spring 项目中需要将配置文件的属性注入到类的静态字段中 例如:文件上传 //文件上传指定上传位置 //resource-dev.properties 有如下参数 #upload UPLOAD ...

  7. C# 类中的静态字段始终继承自基类

    我们试想一下现在有一个类Parent,它有一个static的int类型字段number,然后如果类Parent有三个子类Child01.Child02和Child03,那么改变Parent.numbe ...

  8. Fsharp 类中的空字段

    fsharp设计之初就尽可能的避免使用null.在我的编程经验中null真是个错误之源,垃圾代码之源,95%的系统奔溃之源.其实在设计之初就应该考虑你的系统需要null表现什么?是未初始化的状态,还是 ...

  9. 利用反射拿到并递归C#类中的各个字段名字及类型

       以下方法实现了遍历一个class中所有的字段, 并且递归遍历sub class.  private StringBuilder _properties = new StringBuilder() ...

随机推荐

  1. docker mysql 容器报too many connections 引发的liunx磁盘扩容操作

    症状每次删除mysql容器重启没两分钟又报标题错 df -h 命令查看各个挂载空间应用情况发现root home var 三个文件目录挂载的空间满了 网上百度了一下liunx磁盘扩容操作,fdisk ...

  2. discuz 设置

    veikei_dz_com_20120821_gray_free 模板  论坛 瀑布流 需要在后台设置  论坛--板块管理--编辑对应板块--扩展设置--开启图片列表模式 FAQ 帮助页面 在后台管理 ...

  3. htc 手机

    是否解锁locked unlocked 然后刷入REC

  4. mysql8无法用navicat连接(mysql8加密方式的坑)

    关键词:mysql8无法用navicat连接,navicat无法连接mysql8,mysql8,mysql8的加密方式 [1]mysql8 的坑 密码加密规则 在MySQL 8.0.以上版本中,cac ...

  5. python 传址 与传值(暂时保存,后续做分类)

    # python 传址 与 传值 # 定义一个函数 用于将 list 下标为2的数据加100 并进行打印 该list def test1(list): list[2]+=100 print(list) ...

  6. Oracle 单实例安装篇

    linux中启动网卡报错:Error: Connection activation failed: Connection 'eth0' is not available on the device e ...

  7. vue-loader介绍和单页组件介绍

    $ \es6\sing-file>  npm install vue-loader@14.1.1 -D vue-template-compiler@2.5.17 -D npm install v ...

  8. c++实现直接插入排序

    基本概念 直接插入排序是一种最简单的排序方法,排序过程为:先将第一个元素看作是只有一个元素的有序子表,然后从第二个元素开始,将待排序元素依次插入到前面有序的子表中,直到全部排序完毕.在整个过程中,前面 ...

  9. Linux的环境变量.bash_profile .bashrc profile文件

    Shell变量有局部变量.环境变量之分.局部变量就是指在某个Shell中生效的变量,只在此次登录中有效.环境变量通常又称“全局变量”,虽然在Shell中变量默认就是全局的,但是为了让子Shall继承当 ...

  10. 1.基础CRUD

    在ef中,CUD都使用Datacontext.SaveChange()进行保存. SavaChange方法在保存之前会自动调用DetectChanges方法检查DataContext中做了什么更改,以 ...