使用@JsonView注解控制返回的Json属性
我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高。还是记录以下,万一今后用到了呢
⒈在实体类中使用接口来声明该实体类的多个视图。
⒉在实体类的属性get方法上指定该属性在那个视图中呈现。
package cn.coreqi.security.entities;
import com.fasterxml.jackson.annotation.JsonView;
public class User {
public interface UserSimpleView{}; //简单视图
public interface UserDetailView extends UserSimpleView{}; //高级视图
private Long id;
private String username;
private String password;
private Integer enabled;
public User() {
}
public User(Long id, String username, String password, Integer enabled) {
this.id = id;
this.username = username;
this.password = password;
this.enabled = enabled;
}
@JsonView(UserSimpleView.class)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@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;
}
@JsonView(UserSimpleView.class)
public Integer getEnabled() {
return enabled;
}
public void setEnabled(Integer enabled) {
this.enabled = enabled;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}
}
⒊在控制器的Action方法上使用@JsonView注解声明该Action返回的实体类视图。
package cn.coreqi.security.controller; import cn.coreqi.security.entities.User;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List; @RestController
public class UserController {
@GetMapping("/users")
@JsonView(User.UserSimpleView.class)
public List<User> query(){
List<User> users = new ArrayList<>();
users.add(new User(1L,"fanqi","fanqi",1));
users.add(new User(2L,"fanqi","fanqi",1));
users.add(new User(3L,"fanqi","fanqi",1));
return users;
} @GetMapping("/user/{id:\\d+}") //使用正则指定Id为数字
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable String id){
return new User(1L,"fanqi","fanqi",1);
}
}
使用@JsonView注解控制返回的Json属性的更多相关文章
- @JsonView注解指定返回的model类中显示的字段
1.User类 package com.imooc.model; import com.fasterxml.jackson.annotation.JsonView; /** * @author oy ...
- ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...
- SpringMvc返回报文形式的控制-验证方法: JSON or HTML or XML
首先,请求通过accept请求头声明了支持的返回格式 然后,框架根据该请求头和代码实现(注解)选择了对应的MessageConverter处理返回! 一.验证过程 1.返回html 1.1.请求组装 ...
- SpringMVC 返回的 json 中去除某些不必要的属性
修改返回的Model,在对应的属性的get方法上使用 com.fasterxml.jackson.annotation.JsonIgnore 注解即可. 如 @JsonIgnore(true) pub ...
- 解决@ResponseBody注解返回的json中文乱码问题
1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...
- 控制类名(className 属性)设置或返回class属性
控制类名(className 属性) className 属性设置或返回元素的class 属性. 语法: object.className = classname 作用: 1.获取元素的class 属 ...
- jquery ajax 返回的json对象 新增属性值(干货)
$.ajax({ type:"GEt'; url:"你的地址", data:{"你的字段","字段值"} success:funt ...
- jackSon注解– @JsonInclude 注解不返回null值字段
@Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...
- 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...
随机推荐
- rabbitMQ 3.6.15生产环境
服务器配置 系统环境:CentOS 7 由于RabbitMQ依赖erlang, 所以需要先安装erlang 下载erlang 下载地址 http://erlang.org/download/ linu ...
- Druid数据源配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-met ...
- QT: 自定义断言;
使用Qt creator + mingw + gdb进行qt项目开发时,应用Q_ASSERT进行断言总是会出现问题: 断言失败,程序崩溃而不是停止: 采用自定义断言能完美解决该问题(方法取自于国外 ...
- BashOnWindow安装mysql
1.下载mysql 服务器和客户端 sudo apt-get install mysql-server mysql-client 其中过程会让输入用户名和密码 2.启动mysql服务 sudo ser ...
- GitHub合并(merge)代码时冲突解决
1.手动merge-->消除冲突-->然后commit,push 2.每次合并代码之前需要从远程主分支上拉取代码, 3.使用git命令行解决冲突. 新手可参考一些博客https://www ...
- 2017-12-14python全栈9期第一天第六节之用户交互
9,用户交互.input 1,等待输入, 2,将你输入的内容赋值给了前面变量. 3,input出来的数据类型全部是str 10,基础数据类型初始.数字:int 12,3,45 + - * / ** % ...
- PSi-Population Stability Index (PSI)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- Shell中引号的操作
单引号.双引号.反撇号的作用与区别 单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动的使用,唯一需要注意的点是不允许引用自身: 示例如下: sh-4.2# echo ...
- cors解决跨域
什么是cors CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpReq ...
- UDP协议
本文分析基于Linux Kernel 1.2.13 原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/7532512 更多请看专栏, ...