ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-06-23 17:41:57  阅读:176  来源: 互联网

标签:username springframework class JsonView import org model public 类中


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(UserSimpleView.class)后,测试打印结果:mvcResult: [{"username":"张三","password":"123"},{"username":"李四","password":"123"}]

标签:username,springframework,class,JsonView,import,org,model,public,类中
来源: https://www.cnblogs.com/xy-ouyang/p/11073473.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有