@JsonView注解指定返回的model类中显示的字段
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类中显示的字段的更多相关文章
- 项目实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错,eclipse中配置lombok
@Data注解来源与Lombok,可以减少代码中大量的set get方法,大量减少冗余代码,但是今天部署项目时候,发现实体类使用@Data注解,但是项目业务类中使用getA(),setA()方法报错. ...
- 使用@JsonView注解控制返回的Json属性
我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高.还是记录以下,万一今后用到了呢 ⒈在实体类中使用接口来声明该实体类的多个视图. ⒉在实体类的属性get方 ...
- KVC在定义Model类中的妙用
@我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ...
- Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效
@Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...
- django的Model 模型中常用的字段类型
常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...
- spring 项目中在类中注入静态字段
有时spring 项目中需要将配置文件的属性注入到类的静态字段中 例如:文件上传 //文件上传指定上传位置 //resource-dev.properties 有如下参数 #upload UPLOAD ...
- C# 类中的静态字段始终继承自基类
我们试想一下现在有一个类Parent,它有一个static的int类型字段number,然后如果类Parent有三个子类Child01.Child02和Child03,那么改变Parent.numbe ...
- Fsharp 类中的空字段
fsharp设计之初就尽可能的避免使用null.在我的编程经验中null真是个错误之源,垃圾代码之源,95%的系统奔溃之源.其实在设计之初就应该考虑你的系统需要null表现什么?是未初始化的状态,还是 ...
- 利用反射拿到并递归C#类中的各个字段名字及类型
以下方法实现了遍历一个class中所有的字段, 并且递归遍历sub class. private StringBuilder _properties = new StringBuilder() ...
随机推荐
- appium+python+安卓模拟器环境搭建和启动app实例
本文主要介绍在windows环境下搭建app自动化测试环境,具体步骤如下: 1.下载安卓sdk,网上很多资源 2.下载并安装安卓模拟器,官网上有 删除bin文件下的adb.exe和nox_adb.ex ...
- 在CentOS 8 Linux中安装使用Cockpit服务器管理软件
在本文中,我们将帮助您在CentOS 8服务器中安装Cockpit Web 控制台,以管理和监视本地系统以及网络环境中的Linux服务器.您还将学习如何将远程Linux主机添加到Cockpit并在Ce ...
- HDU 1003 Max Sum (动态规划 最大区间和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Solr 4.4.0增加core
假设现在我们现在需要增加一个新的core名称为"core1" 1. 在solr_home目录新建目录core1 $ mkdir /usr/local/contentplatform ...
- [洛谷P4183][USACO18JAN]Cow at Large P
题目链接 Bzoj崩了之后在洛谷偶然找到的点分好题! 在暴力的角度来说,如果我们$O(n)$枚举根节点,有没有办法在$O(n)$的时间内找到答案呢? 此时如果用树形$dp$的想法,发现是可做的,因为可 ...
- noip2018day1-赛道修建
题目描述 \(C\) 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 $m $条赛道. \(C\) 城一共有 \(n\) 个路口,这些路口编号为 \(1,2,-,n\)有 $n-1 $条适合于修 ...
- Python基础数据类型str字符串
3.3字符串str ' ' 0 切片选取 [x:y] 左闭右开区间 [x:y:z] 选取x到y之间 每隔z选取一次(选取x,x+z,....) z为正 索引位置:x在y的左边 z为负 索引位置:x在y ...
- 一个Accecc_Token生成和缓存和读取类,微信/小程序开发必须学
Access_Token是调用微信和小程序各种接口的临时凭证,有效期2小时(7200秒),很多接口都需要调用access_token接口生成一个access_token的,例如微信支付,微信分享,公众 ...
- php前台表单限制PHP上传大小
在php文件上传时候,一般我都认为考虑php.ini配置修改文件上传大小,还后台控制上传大小,这里教你php前台表单限制PHP上传大小 <form action="http://www ...
- [Vue] vue-router-interview
1.vue-router 怎么重定向页面? 路由中配置 redirect 属性 使用路由的别名来完成重定向 2.vue-router 怎么配置 404 页面? path: '*',放在最后一个 3.切 ...