@PostMapping("/test") @ResponseBody public String test() { InputStream test = getClass().getResourceAsStream("/static/json/test.json"); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "a"); map1.put("age", 10); JSONObject json1 = new JSONObject(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", 1); map2.put("age", "12"); JSONObject json2 = new JSONObject(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "a"); map3.put("sex", "0"); JSONObject json3 = new JSONObject(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("name", "a"); map4.put("age", 11); map4.put("sex", 0); JSONObject json4 = new JSONObject(map4); Map<String, Object> map5 = new HashMap<String, Object>(); map5.put("name", "ad"); map5.put("age", "12"); map4.put("sex", 0); JSONObject json5 = new JSONObject(map5); JSONObject Schema = new JSONObject(new JSONTokener(test)); Schema schema = SchemaLoader.load(Schema); System.out.println("—————————data1———————————"); try { schema.validate(json1); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data2———————————"); try { schema.validate(json2); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data3———————————"); try { schema.validate(json3); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data4———————————"); try { schema.validate(json4); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println("—————————data5———————————"); try { schema.validate(json5); System.out.println("校验成功!"); } catch (ValidationException e) { System.out.println(e.getAllMessages()); } return "hello"; }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/11287.html