@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() ...
随机推荐
- 【神经网络与深度学习】Caffe使用step by step:caffe框架下的基本操作和分析
caffe虽然已经安装了快一个月了,但是caffe使用进展比较缓慢,果然如刘老师说的那样,搭建起来caffe框架环境比较简单,但是完整的从数据准备->模型训练->调参数->合理结果需 ...
- Spring Cloud 使用Feign调用服务传递Header中的参数
1.使用Feign 调用其他微服务,尤其是在多级调用的同时,需要将一些共同的参数传递至下一个服务,如:token.比较方便的做法是放在请求头中,在Feign调用的同时自动将参数放到restTempla ...
- Editor placeholder in source code错误
When you insert code via autocompletion (or via a code snippet, sometimes), there may be placeholder ...
- [转帖]VPS、虚拟主机、云主机的区别
引用知乎网友通俗的例子解释: https://www.cnblogs.com/fjping0606/p/9993849.html 其实 应该是 vps 只是 单台物理机上面的虚拟机 云主机 可能是资源 ...
- Spring boot启动后没有生成日志文件问题排错
我的配置是: logging.file.name=spring-boot.log logging.file.path=D:/log/ 系统启动后日志文件没有生成 原因:一开始以为这两个属性是配合着使用 ...
- redis 命令都在这了
DEL key [key ...]删除指定的key(一个或多个) DUMP key导出key的值 EXISTS key [key ...]查询一个key是否存在 EXPIRE key seconds设 ...
- fatal: refusing to merge unrelated histories问题解决
git中pull或merge时偶尔出现
- vim学习(一)之简介、安装、配置
vim简介 Vim是从 vi 发展出来的一个文本编辑器,是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面. 简单的来说, vi 是老式的文字处理器,不过功能已经很齐全了,但是还是 ...
- 生成二维码功能(js前端)
生成二维码需要引入qrcode.js和jquery.min.js <!DOCTYPE html> <head> <title>二维码</title> & ...
- Spring Boot自动配置总结
Spring Boot项目启动的时候加载主配置类,并开启了自动配置功能.(Spring Boot的自动配置功能是Spring Boot的一大重要且突出的特性) 那么我们需要了解下它: 如何加载主配置类 ...