/** * 得到指定图书编号的图书信息 * ajax请求该方法 * 返回该图书信息的json对象 * @return */ public String getBook(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); Book book = new Book(); book.setBookId(bookId); Book newBook = bookService.getBookById(book);//得到图书 JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Authorization||name.equals("authorization")){ return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newBook,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
public String getForfeitInfoById(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); ForfeitInfo forfeitInfo = new ForfeitInfo(); forfeitInfo.setBorrowId(borrowId); ForfeitInfo newForfeitInfo = forfeitService.getForfeitInfoById(forfeitInfo); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Authorization||name.equals("authorization") || obj instanceof Set || name.equals("borrowInfos")){ return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newForfeitInfo,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
public String getAuthorization(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); Authorization authorization = new Authorization(); authorization.setAid(id); Authorization newAuthorization = authorizationService.getAuthorizationByaid(authorization); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Admin || name.equals("admin")){//过滤掉Authorization中的admin return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newAuthorization,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
public String getBackInfoById(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); BackInfo backInfo = new BackInfo(); backInfo.setBorrowId(borrowId); BackInfo newBackInfo = backService.getBackInfoById(backInfo); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Authorization||name.equals("authorization") || obj instanceof Set || name.equals("borrowInfos")){ return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newBackInfo,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
/** * 根据借阅id查询该借阅信息 * @return */ public String getBorrowInfoById(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); BorrowInfo info = new BorrowInfo(); info.setBorrowId(borrowId); BorrowInfo newInfo = borrowService.getBorrowInfoById(info); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Authorization||name.equals("authorization") || obj instanceof Set || name.equals("borrowInfos")){ return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newInfo,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
/** * トリガー情報を取得します。<br/> * トリガー情報が存在しない場合、空のリストを返します。 * * @param param {@link TriggerGetParam} * @return 取得したトリガー情報のリスト */ @SuppressWarnings("unchecked") public List<Trigger> get(TriggerGetParam param) { JSONObject params = JSONObject.fromObject(param, defaultConfig); JSONArray result = (JSONArray) accessor.execute("trigger.get", params); JsonConfig config = defaultConfig.copy(); config.setCollectionType(List.class); config.setRootClass(Trigger.class); config.setJavaPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source, String name, Object value) { if ("hosts".equals(name)) { return true; } return false; } }); return (List<Trigger>) JSONArray.toCollection(result, config); }
/** * アイテム情報を取得します。<br/> * アイテム情報が存在しない場合、空のリストを返します。 * * @param param {@link ItemGetParam} * @return 取得したアイテム情報のリスト */ @SuppressWarnings("unchecked") public List<Item> get(ItemGetParam param) { JSONObject params = JSONObject.fromObject(param, defaultConfig); JSONArray result = (JSONArray) accessor.execute("item.get", params); JsonConfig config = defaultConfig.copy(); config.setCollectionType(List.class); config.setRootClass(Item.class); config.setJavaPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source, String name, Object value) { if ("hosts".equals(name)) { return true; } return false; } }); return (List<Item>) JSONArray.toCollection(result, config); }
public String getStates() throws Exception { try { String code = getRequest().getParameter("code"); Dictvalue dictvalue = dictvalueManager.findByCode(code); List list = new ArrayList(dictvalue.getDictvalues()); JsonConfig cfg = new JsonConfig(); cfg.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if (name.equals("code") || name.equals("value")) { return false; } else { return true; } } }); String ss = JSONArray.fromObject(list, cfg).toString(); StringBuffer returnstr = new StringBuffer(""); returnstr.append(ss); super.renderHtmlUTF(returnstr.toString()); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 得到图书类型 * ajax请求该方法 * 返回图书类型集合的json对象 * @return */ public String getBookType(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); BookType bookType = new BookType(); bookType.setTypeId(id); BookType newType = bookTypeService.getBookTypeById(bookType); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Set||name.equals("books")){//过滤掉集合 return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newType,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
/** * 得到指定的读者 * @return */ public String getReader(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); Reader reader = new Reader(); reader.setReaderId(readerId); Reader newReader = readerService.getReaderById(reader); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Set||name.equals("borrowInfos") || obj instanceof Authorization || name.equals("authorization") ){//过滤掉集合 return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newReader,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
/** * 得到指定的普通管理员 * Ajax请求该方法 * 向浏览器返回该管理员的json对象 * @return */ public String getAdmin(){ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=utf-8"); Admin admin = new Admin(); admin.setAid(id); Admin newAdmin = adminService.getAdminById(admin); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(name.equals("admin")){//过滤掉Authorization中的admin return true; }else{ return false; } } }); JSONObject jsonObject = JSONObject.fromObject(newAdmin,jsonConfig); try { response.getWriter().print(jsonObject); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } return null; }
@Test public void testFindBookType(){ SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory"); Session session = sessionFactory.openSession(); String hql= "from BookType"; List createQuery = session.createQuery(hql).list(); BookType bookType = (BookType) createQuery.get(0); System.out.println(bookType); // Set<Book> books = bookType.getBooks(); // System.out.println(books.size()); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object obj, String name, Object value) { if(obj instanceof Set||name.equals("books")){ return true; }else{ return false; } } }); String json = JSONArray.fromObject(createQuery,jsonConfig).toString();//List------->JSONArray System.out.println(json); }
/** * 为JsonConfig添加去除null属性的功能 * @return */ public JsonConfigs filterNullProp() { //去除字段为null值得属性 this.setJsonPropertyFilter(new PropertyFilter(){ @Override public boolean apply(Object source, String name, Object value) { return value == null; } }); return this; }
public CustomJsonConfig(Class clazz,final String exclude){ this.clazz = clazz; setRootClass(clazz); setJavaPropertyFilter(new PropertyFilter() { public boolean apply(Object arg0, String param, Object arg2) { if(param.equals(exclude))return true; return false; } }); }
public CustomJsonConfig(Class clazz,final String[] excludes){ this.clazz = clazz; setRootClass(clazz); setJavaPropertyFilter(new PropertyFilter() { public boolean apply(Object arg0, String param, Object arg2) { for(String exclude:excludes){ if(param.equals(exclude))return true; } return false; } }); }
public static JsonConfig propertiesFilter(final String... names) { final List<String> nameList = Arrays.asList(names); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source, String name, Object value) { if (nameList.contains(name)) { return true; } return false; } }); return jsonConfig; }
public static JsonConfig propertiesFilter(final Map<Class<?>, List<String>> clazzNamesMap) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source, String name, Object value) { if (clazzNamesMap.containsKey(source.getClass()) && clazzNamesMap.get(source.getClass()).contains(name)) { return true; } return false; } }); return jsonConfig; }
public String getDept(){ try{ String userId = this.getRequest().getParameter("tId"); List<Organization> Organizations = this.organizationManager.findByUserId(userId); JsonConfig cfg = new JsonConfig(); cfg.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { if (name.equals("id") || name.equals("name")) { return false; } else { return true; } } }); String ss = JSONArray.fromObject(Organizations, cfg).toString(); StringBuffer returnstr = new StringBuffer(""); returnstr.append(ss); super.renderHtmlUTF(returnstr.toString()); }catch(Exception e){ e.printStackTrace(); } return null ; }
@Transient public String getParametrosAsJSON() { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { public boolean apply(Object source, String name, Object value) { return (value != null && name.equals("passo")); } }); return JSONArray.fromObject(getParametros(), jsonConfig).toString(); }