标签:dir xtend length contexts and error url lis webapp
在某一些请求返回的JSON中,我们并不希望返回pojo中的某些字段或全部字段。而在另一些请求中需要返回某些字段。
@JsonView
的使用步骤
2.2.1User
对象定义
public class User{ /** * 用户简单视图 */ public interface UserSimpleView{}; /** * 用户详情视图 */ public interface UserDetailView extends UserSimpleView{}; private String username; private String password; private String age; @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 String getAge() { return age; } public void setAge(String age) { this.age = age; } }
UserSimpleView
和UserDetailView
,UserDetailView
继承UserSimpleView
,UserDetailView
拥有视图UserSimpleView
的属性UserController
@RestController public class UserController { @GetMapping("/user") @JsonView({User.UserSimpleView.class}) public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(size = 10,page=1,sort = {"username","age"},direction = Sort.Direction.DESC) Pageable pageable){ System.out.println(userQueryCondition); System.out.print(pageable.getPageSize()); System.out.println(pageable.getSort()); System.out.println(pageable.getOffset()); List<User> users = new ArrayList<>(); users.add(new User()); users.add(new User()); users.add(new User()); return users; } /** * 在url中使用正则表达式 * @param id * @return */ @GetMapping("/user/{id:\\d+}") @JsonView(User.UserDetailView.class) public User get(@PathVariable String id){ System.out.println(id); User user = new User(); user.setUsername("tom"); return user; } }
@RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void setup(){ //根据webApplicationContext构建mockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void whenQuerySuccess() throws Exception { String result = mockMvc.perform(MockMvcRequestBuilders.get("/user") .param("username","tom") .param("age","11") .param("ageTo","30") .param("page","20") .param("pageSize","100") .param("sort","age,desc") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3)) .andReturn().getResponse().getContentAsString(); System.out.println(result); } /** * 请求成功逻辑测试 * @throws Exception */ @Test public void wherGetSuccess() throws Exception { String result = mockMvc.perform(MockMvcRequestBuilders.get("/user/1") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.username").value("tom")) .andReturn().getResponse().getContentAsString(); System.out.println(result); } /** * 路径正则表达式的匹配规则测试 * @throws Exception */ @Test public void whenGetFail() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/user/a") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().is4xxClientError()); } }
password
字段[{"username":null,"age":null},{"username":null,"age":null},{"username":null,"age":null}]
password
字段{"username":"tom","password":null,"age":null}
标签:dir xtend length contexts and error url lis webapp
原文地址:https://www.cnblogs.com/dw3306/p/10925431.html