@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() ...
随机推荐
- 《Python编程从0到1》笔记4——你分得清“索引和切片”吗?
Python为序列类型(sequence types)[1]提供了独特的索引(indexing)和切片(slicing)机制以访问序列的某个元素或某一部分. [1] 如list, tuple, ran ...
- 深入理解java:4. 框架编程
了解 Servlet 和 Filter Servlet(即servlet-api.jar) 是 J2EE 最重要的一部分,有了 Servlet 你就是 J2EE 了,J2EE 的其他方面的内容择需采用 ...
- 【转】MySQL-Utilities,mysql工具包
原文:https://blog.csdn.net/leshami/article/details/52795777 MySQL Utilities 是一组基于python语言编写的python库的命令 ...
- MySQL_基础
## 数据库的基本概念 1. 数据库的英文单词: DataBase 简称 : DB 2. 什么数据库? * 用于存储和管理数据的仓库. 3. 数据库的特点: 1. 持久化存储数据的.其实数据库就是一个 ...
- 小记---------maxwell启动闪退问题
日志报错信息如下:大致是说因为maxwell在对接mysql时伪装成一个从库slave,但是uuid重复.猜想是其他部门同事也在同时使用maxwell,都使用的是maxwell默认的uuid ,从而导 ...
- Java 读取Json文件内容
读取json文件为String类型: import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logge ...
- 洛谷 P2647 最大收益 题解
题面 对于“n个物品选任意个”我们就可以想到一种递推方法,即设f[i][j]表示前i个物品选j个的最大收益 我们发现正着转移并不好转移,我们可以倒着转移,使选择的当前第i号物品为第一个物品,这样的话我 ...
- Comet OJ - Contest #14
Rank38. 还是比较不满意吧,C卡了太久,E没调出来,D也没空去做了. A 签到题. #include<bits/stdc++.h> using namespace std; #def ...
- 抖音很火的存钱计划,让python告诉你总共可以存到多少钱!
抖音上有个很火的存钱计划,说是第一天存1块钱,第二天存2块钱,第三天存3块钱.....依此类推存365天,总共可以存到多少钱,我们现在用python告诉你怎么做: #定个初始存入金额 money = ...
- jsp+servlet实现文件上传下载
相关素材下载 01.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...