当前位置:网站首页 > Java基础 > 正文

java json schema教程



 @PostMapping(“/test”)

@ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String test() { InputStream test </span>= getClass().getResourceAsStream("/static/json/test.json"<span style="color: rgba(0, 0, 0, 1)">); Map</span>&lt;String, Object&gt; map1 = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">(); map1.put(</span>"name", "a"<span style="color: rgba(0, 0, 0, 1)">); map1.put(</span>"age", 10<span style="color: rgba(0, 0, 0, 1)">); JSONObject json1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject(map1); Map</span>&lt;String, Object&gt; map2 = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">(); map2.put(</span>"name", 1<span style="color: rgba(0, 0, 0, 1)">); map2.put(</span>"age", "12"<span style="color: rgba(0, 0, 0, 1)">); JSONObject json2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject(map2); Map</span>&lt;String, Object&gt; map3 = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">(); map3.put(</span>"name", "a"<span style="color: rgba(0, 0, 0, 1)">); map3.put(</span>"sex", "0"<span style="color: rgba(0, 0, 0, 1)">); JSONObject json3 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject(map3); Map</span>&lt;String, Object&gt; map4 = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">(); map4.put(</span>"name", "a"<span style="color: rgba(0, 0, 0, 1)">); map4.put(</span>"age", 11<span style="color: rgba(0, 0, 0, 1)">); map4.put(</span>"sex", 0<span style="color: rgba(0, 0, 0, 1)">); JSONObject json4 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject(map4); Map</span>&lt;String, Object&gt; map5 = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String, Object&gt;<span style="color: rgba(0, 0, 0, 1)">(); map5.put(</span>"name", "ad"<span style="color: rgba(0, 0, 0, 1)">); map5.put(</span>"age", "12"<span style="color: rgba(0, 0, 0, 1)">); map4.put(</span>"sex", 0<span style="color: rgba(0, 0, 0, 1)">); JSONObject json5 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONObject(map5); JSONObject Schema </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> JSONObject(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JSONTokener(test)); Schema schema </span>=<span style="color: rgba(0, 0, 0, 1)"> SchemaLoader.load(Schema); System.out.println(</span>"—————————data1———————————"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { schema.validate(json1); System.out.println(</span>"校验成功!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println(</span>"—————————data2———————————"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { schema.validate(json2); System.out.println(</span>"校验成功!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println(</span>"—————————data3———————————"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { schema.validate(json3); System.out.println(</span>"校验成功!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println(</span>"—————————data4———————————"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { schema.validate(json4); System.out.println(</span>"校验成功!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ValidationException e) { System.out.println(e.getAllMessages()); } System.out.println(</span>"—————————data5———————————"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { schema.validate(json5); System.out.println(</span>"校验成功!"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (ValidationException e) { System.out.println(e.getAllMessages()); } </span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello"<span style="color: rgba(0, 0, 0, 1)">; 

}

版权声明


相关文章:

  • java分层架构教程2025-12-03 13:58:04
  • 大一java教程2025-12-03 13:58:04
  • 低频红石教程java2025-12-03 13:58:04
  • java渗透 教程2025-12-03 13:58:04
  • java教程42025-12-03 13:58:04
  • Java redis实战教程2025-12-03 13:58:04
  • java异步文件教程2025-12-03 13:58:04
  • 算法很美java教程99讲2025-12-03 13:58:04
  • java教程dvd光盘2025-12-03 13:58:04
  • java高级教程16节2025-12-03 13:58:04