我也是刚看到原来还可以这么玩,但是我还是习惯使用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属性的更多相关文章

  1. @JsonView注解指定返回的model类中显示的字段

    1.User类 package com.imooc.model; import com.fasterxml.jackson.annotation.JsonView; /** * @author oy ...

  2. ASP.NET Web API 通过参数控制返回类型(JSON|XML)

    一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml. 因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的 ...

  3. SpringMvc返回报文形式的控制-验证方法: JSON or HTML or XML

    首先,请求通过accept请求头声明了支持的返回格式 然后,框架根据该请求头和代码实现(注解)选择了对应的MessageConverter处理返回! 一.验证过程 1.返回html 1.1.请求组装 ...

  4. SpringMVC 返回的 json 中去除某些不必要的属性

    修改返回的Model,在对应的属性的get方法上使用 com.fasterxml.jackson.annotation.JsonIgnore 注解即可. 如 @JsonIgnore(true) pub ...

  5. 解决@ResponseBody注解返回的json中文乱码问题

    1. 简介 主要解决@ResponseBody注解返回的json中文乱码问题. 2.解决方案 2.1mvc加上注解(推荐此方法) 在mvc配置文件中假如下面配置(写在 <mvc:annotati ...

  6. 控制类名(className 属性)设置或返回class属性

    控制类名(className 属性) className 属性设置或返回元素的class 属性. 语法: object.className = classname 作用: 1.获取元素的class 属 ...

  7. jquery ajax 返回的json对象 新增属性值(干货)

    $.ajax({ type:"GEt'; url:"你的地址", data:{"你的字段","字段值"} success:funt ...

  8. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  9. 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面

    这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...

随机推荐

  1. MySQL数据库详解之"双1设置"的数据安全的关键参数案例分享

    mysql的"双1验证"指的是innodb_flush_log_at_trx_commit和sync_binlog两个参数设置,这两个是是控制MySQL 磁盘写入策略以及数据安全性 ...

  2. POJ3417 LCA+树dp

    http://poj.org/problem?id=3417 题意:先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂 ...

  3. i2c框架

    目录 i2c框架 寄存器 主机发送 主机接收 中断处理 程序框架 title: iic框架 tags: ARM date: 2018-11-05 13:44:58 --- i2c框架 寄存器 /* 配 ...

  4. Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小

    Linux网卡调优篇-禁用ipv6与优化socket缓冲区大小 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一般在内网环境中,我们几乎是用不到IPV6,因此我们没有必要把多不 ...

  5. Spring Boot笔记七:扩展Spring MVC

    新建一个类,继承WebMvcConfigurerAdapter package com.vae.springboot.config; import org.springframework.contex ...

  6. Windows-kms

    系统下载 Windows 长期服务版 Windows 10 Enterprise LTSC 2019 (x64) - DVD (Chinese-Simplified) 文件名 cn_windows_1 ...

  7. JAVA核心技术I---JAVA基础知识(数据结构基础)

    一:数组 (一)基本内容是与C一致的 (二)数组定义和初始化 (1)声明 int a[]; //a没有new操作,没有被分配内存,为null int[] b; //b没有new操作,没有被分配内存,为 ...

  8. python 面向对象(三)类与类之间的关系 初始化方法一些类

    ###################总结################# 面试的时候 让写python一些特殊方法 __init__ 创建对象的时候初始化 __new__对象实例化调用第一个方法 ...

  9. Golang入门教程(十)内建函数

    比较常用的内建函数 参考: http://blog.csdn.net/liumiaocn/article/details/54804074

  10. Django多表操作

    多表创建 创建模型 下面通过一个简单的图书管理系统,来阐述多表的创建和查询操作 在视图函数里里定义如下代码 from django.db import models class Book(models ...