首页 > 代码库 > spring mvc 封装json对象

spring mvc 封装json对象

@RequestMapping("/findByItem")
public void findByItem(int pageNO,String userName,Date startDate,Date endDate,HttpServletResponse response)throws Exception{
CutPageBean cutBean = service.findByItem(pageNO, userName, startDate, endDate);
//创建jackson对象
ObjectMapper om = new ObjectMapper();
//创建输出属性过滤器
SimpleFilterProvider filterProvider = new SimpleFilterProvider().
setFailOnUnknownId(false);
//定义指定实体bean需要转化的属性列表
filterProvider.addFilter("ub", SimpleBeanPropertyFilter.  //ub为定义的要过滤的javabean对象 详细见下
filterOutAllExcept("id","userName","birthday"));

om.setFilters(filterProvider);

//将指定对象转换为json格式发送到客户端
om.writeValue(response.getWriter(), cutBean);
}

数据javabean

@Entity
@Table(name="t_user")
@JsonFilter("ub") //指定json的过滤器ID 调用时就会过滤此ID的类
public class UserBean {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String userName;
private Date birthday;

spring mvc 封装json对象