SpringBoot查询的结果按视图结果展示结果
现在有这样一个需求,当查询用户信息列表的时候不需要将用户的密码展示出来。
而根据用户的Id查询用户信息,就应该把密码带上。
@JsonView 的用途:可以为Json字符串和java对象之间的转换提供一个视图。
先贴出来用户的实体类:
package com.imooc.dto; import java.sql.Date; import javax.validation.constraints.Past; import org.hibernate.validator.constraints.NotBlank; import com.fasterxml.jackson.annotation.JsonView; import com.imooc.vailator.MyConstraint; public class User { public interface UserSimpleView { }; public interface UserDetailView extends UserSimpleView { }; private String id; private String username; private String password; private Date birthday; @JsonView(UserSimpleView.class) public String getId() { return id; } public void setId(String 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
可以看出来用户ID,用户名,生日是简单的视图
密码又继承了简单的视图对实体类的采用
package com.imooc.web.controller; /** * @author liuzh * */ @RestController @RequestMapping("/user") public class UserController { @GetMapping @JsonView(User.UserSimpleView.class) @ApiOperation(value = "用户查询服务") public List<User> query(UserQueryCondition condition, Pageable pageable) { System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE)); System.out.println(ReflectionToStringBuilder.toString(pageable, ToStringStyle.MULTI_LINE_STYLE)); List<User> users = new ArrayList<User>(); users.add(new User()); users.add(new User()); users.add(new User()); return users; } @GetMapping("/{id:\\d+}") @JsonView(User.UserDetailView.class) public User getInfo(@ApiParam("用户ID") @PathVariable String id) { // throw new UserNotExistException(id); // throw new RuntimeException("This user in not exist"); System.out.println("进入getinfo服务"); User user = new User(); user.setUsername("tom"); return user; }
使用WireMock进行测试
package com.imooc.web.controller; @RunWith(SpringRunner.class) @SpringBootTest public class UserControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void sertUp() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void whenQuerySuccess() throws Exception { String result = mockMvc .perform(get("/user").param("username", "jojo").param("age", "18").param("ageTo", "60") .param("xxx", "yyy").param("size", "15").param("page", "3").param("sort", "age,desc") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()).andExpect(jsonPath("$.length()").value(3)).andReturn().getResponse() .getContentAsString(); System.out.println(result); } @Test public void whenGetInfoSuccess() throws Exception { String result = mockMvc.perform(get("/user/1").contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isOk()).andExpect(jsonPath("$.username").value("tom")).andReturn().getResponse() .getContentAsString(); System.out.println(result); } @Test public void whenGetInfoFail() throws Exception { mockMvc.perform(get("/user/a").contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().is4xxClientError()); }