对返回结果进行了统一封装,怎么使用@JsonView注解

 @RequiresPermissions({"manager:view"})
    @RequestMapping("/getManagers")
    @JsonView(Manager.simpleManager.class)
    public Result getManagers(){
        Result result = new Result();
        List<Manager> managers = managerService.selectList(null);
        if(managers!=null){
            result.setCode(1);
            result.setData(managers);
            result.setMsg("查询成功");
        } else {
            result.setCode(0);
            result.setMsg("查询失败");
        }
        return result;
    }

封装的返回

public class Result {
    private String msg;
    private Integer code;
    private Object data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

只有把返回改成List才有效,我这种情况应怎么处理?

     * 查询所有管理员
     * @return
     */

    @RequiresPermissions({"manager:view"})
    @RequestMapping("/getManagers")
//    @JsonView(Manager.simpleManager.class)
    public Result getManagers(){
        Result result = new Result();
        JsonConfig jc=new JsonConfig();
        // 使用setExcludes方法过滤,例如我不想要对象中的roleinfo和departmentInfo:
        jc.setExcludes(new String[]{"password","salt","roles","roleSet","shops","permissionsSet"});
        // 最后转换出来的字符串就会过滤掉我们不要的属性。
        List<Manager> managers = managerService.selectList(null);
        String newArray = JSONArray.fromObject(managers,jc).toString();
        if(managers!=null){
            result.setCode(1);
            result.setData(JSONObject.parse(newArray));
            result.setMsg("查询成功");
        } else {
            result.setCode(0);
            result.setMsg("查询失败");
        }
        return result;
    }

使用JsonConfig过滤解决了

一、@JsonView注解的简介

@JsonView是jackson json中的一个注解,Spring webmvc也支持这个注解,它的作用就是控制输入输出后的json

二、@JsonView注解的使用步骤

1.使用接口来声明多个视图

复制代码
package com.knyel.dto;

public class User {

public interface UserSimpleView {};
public interface UserDetailView extends UserSimpleView {};

private String username;
private  String password;

public String getUsername (){
    return username;
}

public void setUsername (String username){
    this.username = username;
}

public String getPassword (){
    return password;
}

public void setPassword (String password){
    this.password = password;
}

@Override
public String toString (){
    return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}';
}

}
复制代码

2.在值对象的get方法上指定视图

复制代码
package com.knyel.dto;

import com.fasterxml.jackson.annotation.JsonView;

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;
}

@Override
public String toString (){
    return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}';
}

}
复制代码

3.在controller方法上指定视图

复制代码
package com.knyel.wb.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.knyel.dto.User;
import com.knyel.dto.UserQueryCondition;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {
@RequestMapping(value = "/user",method = RequestMethod.GET)
@JsonView(User.UserSimpleView.class)
public List query(UserQueryCondition userQueryCondition, @PageableDefault(page=1,size=10,sort="username,asc") Pageable pageable){
System.out.println(pageable.getPageNumber());
System.out.println(pageable.getSort());
System.out.println(pageable.getPageSize());
System.out.println(userQueryCondition.toString());
List users=new ArrayList<>();
users.add(new User());
users.add(new User());
users.add(new User());
return users;
}
@RequestMapping(value="/user/{id:\d+}",method = RequestMethod.GET)
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable String id){
User user=new User();
user.setUsername("knyel");
System.out.println(user);
return user;
}
}
复制代码