2258 XML JSON Java Script Object Notation title
22/58 Формат на данните • алтернативи: { ▫ XML ▫ JSON (Java. Script Object Notation) "title": "Avengers", "length": 143, "synopsis": …описание на филма… , "cast": [ {"first": "Robert", "last": "Downey Jr. "}, {"first": "Chris", "last": "Evans"}, … ] }
25/58 Java API for JSON Processing • Object Model API: ▫ Json. Reader, Json. Writer ▫ Json. Structure, Json. Object, Json. Array ▫ Json (factory) • Streaming API: ▫ Json. Parser ▫ Json. Generator ▫ Json (factory)
26/58 Пример с Object Model API • четене: Json. Reader reader = Json. create. Reader(new File. Reader("example. json")); Json. Structure structure = reader. read(); if (structure instanceof Json. Object) { Json. Object object = (Json. Object) structure; System. out. println(object. key. Set()); } • писане: Json. Object model = Json. create. Object. Builder(). add("title", "Avengers"). add("length", 143). add("cast", Json. create. Array. Builder(). add(Json. create. Object. Builder(). add("first", "Robert"). add("last", "Downey Jr. ")). build(); Json. Writer writer = Json. create. Writer(System. out); writer. write(model);
27/58 Пример със Streaming API • четене: Json. Parser parser = Json. create. Parser(new File. Reader("example. json")); while (parser. has. Next()) { Json. Parser. Event event = parser. next(); switch (event) { case KEY_NAME: parser. get. String() + " - "); break; case VALUE_FALSE: case VALUE_NULL: case VALUE_TRUE: case VALUE_STRING: case VALUE_NUMBER: System. out. println(parser. get. String()); break; } } други събития: START_ARRAY, END_ARRAY, START_OBJECT, END_OBJECT
28/58 Пример със Streaming API • писане: Json. Generator gen = Json. create. Generator(System. out); gen. write. Start. Object(). write("title", "Avengers"). write("length", 143). write. Start. Array("cast"). write. Start. Object(). write("first", "Robert"). write("last", "Downey Jr. "). write. End(). write. Start. Object(). write("first", "Chris"). write("last", "Evans"). write. End();
30/58 REST имплементации в Java • JAX-RS (JSR 311): ▫ ▫ Jersey (Sun reference implementation) Restlet (http: //www. restlet. org) RESTEasy (JBoss implementation) Apache WINK • Atom • Apache CXF
32/58 Jersey • reference имплементация на JAX-RS • част от Glass. Fish • документация: http: //jersey. java. net
35/58 Примерно REST приложение • регистрира се Jersey контролера <? xml version="1. 0" encoding="UTF-8"? > <web-app …> <display-name>Test. REST</display-name> <servlet-name>Jersey. Controller</servlet-name> <servlet-class> com. sun. jersey. spi. container. servlet. Servlet. Container </servlet-class> </servlet> <servlet-mapping> <servlet-name>Jersey. Controller</servlet-name> <url-pattern>/distributed-systems/*</url-pattern> </servlet-mapping> </web-app>
36/58 Примерно REST приложение @Path("/lectures") public class Distributes. Systems { @GET @Produces("application/json") public String get. Json() { return new Gson(). to. Json(Lecture. Manager. get. Instances()); } @POST @Consumes("application/json") public Response post. Json(String content) { Lecture lecture = new Gson(). from. Json(content, Lecture. class); Lecture. Manager. add. Instance(lecture); URI uri = Uri. Builder. from. Path(lecture. get. ID()). build(lecture); return Response. created(uri). build(); } @Path("{id}") public Lecture get. Lecture(@Path. Param("id") String id) { return Lecture. Manager. get. Instance(id); } }
37/58 Примерно REST приложение public class Lecture { private String id; private String title; private int slide. Count; . . . @GET @Produces("application/json") public String get. Json() { return new Gson(). to. Json(this); } @PUT @Consumes("application/json") public void put. Json(String content) { Lecture modified. Lecture = new Gson(). from. Json(content, Lecture. class); title = modified. Lecture. title; slide. Count = modified. Lecture. slide. Count; } @DELETE public void delete() { Lecture. Manager. remove. Instance(id); } }
45/58 JAX-RS и JAX-B • анотираме POJO клас с JAX-B анотации @Xml. Root. Element public class Lecturer { @Xml. Element(name="first. Name") String first. Name; @Xml. Element(name="last. Name") String last. Name; } @GET @Path("{name}") @Produces("application/xml") public Lecturer get. Lecturer(@Path. Param("{name}") name) { return emps. get(name); }
47/58 WADL пример OPTIONS: http: //localhost: 8080/Test. REST/distributed-systems/lectures/1 <? xml version="1. 0" encoding="UTF-8" standalone="yes"? > <application. . . > <resources base="http: //localhost: 8080/Test. REST/distributed-systems/"> <resource path="lectures/1"> <method id="delete" name="DELETE"/> <method id="get. Json" name="GET"> <response> <representation media. Type="application/json"/> </response> </method> <method id="put. Json" name="PUT"> <request> <representation media. Type="application/json"/> </request> </method> </resources> </application>
49/58 ASF пример <feed xmlns="http: //www. w 3. org/2005/Atom" xml: lang="en" xml: base="http: //opa"> <id>http: //opa/myfeed</id> <title>My Simple Feed</title> <updated>2005 -07 -15 T 12: 00 Z</updated> <link href="/blog" /> <link rel="self" href="/myfeed" /> <entry> <id>http: //opa/entries/1</id> <link rel="edit" href="http: //opa/entries/1" /> <title>A simple blog entry</title> <link href="/blog/2005/07/1" /> <updated>2005 -07 -15 T 12: 00 Z</updated> <summary>This is a simple blog entry</summary> </entry>. . . </feed>
53/58 Аtom. Pub пример - create POST на http: //opa/myfeed с тяло: <entry> <id>http: //opa/entries/2</id> <link rel="edit" href="http: //opa/entries/2" /> <title>Second blog entry</title> <link href="/blog/2005/07/2" /> <updated>2012 -07 -15 T 12: 00 Z</updated> <summary>This is my second entry</summary> </entry>
54/58 Аtom. Pub пример - read GET на http: //opa/entries/2 ще върне резултат: <entry> <id>http: //opa/entries/2</id> <link rel="edit" href="http: //opa/entries/2" /> <title>Second blog entry</title> <link href="/blog/2005/07/2" /> <updated>2012 -07 -15 T 12: 00 Z</updated> <summary>This is my second entry</summary> </entry>
55/58 Аtom. Pub пример – update/delete PUT на edit URI http: //opa/entries/2 с тяло: <entry> <id>http: //opa/entries/2</id> <link rel="edit" href="http: //opa/entries/2" /> <title>Second blog entry</title> <link href="/blog/2005/07/2" /> <updated>2012 -07 -15 T 12: 00 Z</updated> <summary>This is my second entry</summary>. . . допълнителна информация липсваща в feed-a. . . </entry> DELETE на edit URI http: //opa/entries/2 без тяло
- Slides: 58