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"}]