1:编写接口
package com.sh.daniel.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
import com.sh.daniel.dto.User;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping
public List<User> listUsers(@RequestParam String userName){
System.out.println(userName);
List<User> list = new ArrayList<User>();
User user = new User();
user.setUserName(userName);
user.setAge(20);
user.setPassword("22222");
user.setId(11);
list.add(user);
return list;
}
@GetMapping("/{id}")
public User getUserInfo(@PathVariable Integer id){
User user = new User();
user.setId(id);
user.setUserName("Daniel");
user.setAge(20);
user.setPassword("111111");
return user;
}
}
2:编写测试类
package com.sh.daniel;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringSecurityTestApplicationTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void testSimpleQuery() throws Exception {
String result = mockMvc.perform(
get("/user").param("userName", "Daniel")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
@Test
public void testGetDetailInfo() throws Exception {
String result = mockMvc.perform(get("/user/1")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
}
3:编写User实体
package com.sh.daniel.dto; public class User { private Integer id; private String userName; private Integer age; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getAge() { return age; }
4:运行testSimpleQuery()测试方法
可以观察到password密码属性被暴露给了前台,这边想处理,只有在查询用户详细信息的时候才会暴露密码
5:@JsonView的使用步骤
package com.sh.daniel.dto; import com.fasterxml.jackson.annotation.JsonView; public class User { /** * 使用@JsonView来向前台展示字段,包括忽略哪些字段 * 1:定义接口视图 * 2:在get方法上标注哪些get方法属于哪个视图去展示 * 3:在contreoller发方法上注明需要用到的视图 * 这边UserSimpleInfo忽略了password,UserDetailInfo通过继承可以很方便的使用 * UserSimpleInfo定义过的属性,不需要重复去定义 */
public interface UserSimpleInfo{} public interface UserDetailInfo extends UserSimpleInfo{} private Integer id; private String userName; private Integer age; private String password; @JsonView(UserSimpleInfo.class) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @JsonView(UserSimpleInfo.class) public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @JsonView(UserSimpleInfo.class) public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @JsonView(UserDetailInfo.class) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", age=" + age + ", password=" + password + "]"; }
6:在特定的controller上使用不同的视图注解
![]()
7:运行两个test的测试方法第一个方法是忽略掉password字段
![]()
第二个方法显示password字段
![]()