编写restful接口时通过jsonview来控制输出json中字段的显示或者忽略

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字段



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值