13
8/10/2019 1 Principles of Enterprise Web Development Packaging Data for the Web EN 605.681 Overview Both XML and JSON can be used to pass data between remote applications, clients and servers, etc. XML Usually heavier than JSON More difficult to parse JSON Readily parsed by Javascript Faster to process

Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

1

Principles of Enterprise Web Development

Packaging Data for the Web

EN 605.681

Overview

• Both XML and JSON can be used to pass data between remote applications, clients and servers, etc.

• XML – Usually heavier than JSON

– More difficult to parse

• JSON – Readily parsed by Javascript

– Faster to process

Page 2: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

2

XML Overview

• Like HTML based on Standard Generalized Markup Language (SGML)

• XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

– XML documents have a single root tag that contains the remaining content

– Every open tag has a corresponding close tag that must match in name (including case)

– Tags are nested hierarchically – Attribute values must be quoted

• XML comprises many technologies – XML Schema – XSL/XSLT – Extensible Stylesheet Language/Transforms – Xpath – XML Path Language – Others

JSON Overview

• Self-describing format based on syntax used for Javascript objects

– ECMA-262 standard

• Completely language independent

– Easily integrated into Javascript

– Other languages provide supporting APIs (C/C++, Java, ASP, Python, Ruby…)

• Lightweight for rapid processing

Page 3: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

3

XML – JSON Example

<?xml version=“1.0”

encoding=“UTF-8”?>

<authors>

<name>

<firstname>Larry</firstname>

<lastname>Brown</lastname>

</name>

<name>

<firstname>Bob</firstname>

<lastname>Evans</lastname>

</name>

</authors>

235 bytes

{ authors: {

name: [

{

firstname:”Larry”,

lastname:”Brown”

},

{

firstname:”Bob”,

lastname:”Evans”

}

]

}}

146 bytes (38% smaller)

JSON versus XML

• While XML and JSON are both used for the exchange of data over the web, JSON is becoming the defacto format for exchanging data because: – More often JSON offers a more lightweight

representation that improves performance and processing

– Javascript’s built in mapping into objects make it easy to “export” and “import” data from/to the client

• XML as technology still has much to offer that is not available with JSON. For more information see: – Class “Additional Resources”

Page 4: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

4

JSON Syntax

• Data is in name : value pairs (colon delimited) – Names are enclosed in double quotes – Values can be

• Integer or floating point numbers • Strings (enclosed in double quotes) • Boolean (true or false) • Arrays (enclosed in square brackets [] ) • Objects (enclosed in curly brackets {}) • null (all lowercase, represents empty type) • Whitespace (ignored)

• Data pairs are separated by commas • Curly brackets contain objects • Square brackets contain arrays

JSON Examples

• Name : Value Pair – “firstname” : “Richard” – “age” : 30

• Objects – {“firstname”:”Richard”, “lastname”:”Spiegel”, “age”:30 } – {“id”:88421, “Experience”:17, “Reviews”:[8,5,7,4] } – {“InnerObject”: { “name”:”Spiegel”, “years”:10}, “level”:4}

• Arrays [ {“firstname”:”Richard”, “lastname”:”Spiegel”, “age”:30 }, {“firstname”:”Brian”, “lastname”:”Spiegel”, “age”:20}, {“firstname”:”Robert”, “lastname”:”Evand”, “age”:40 } ]

Page 5: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

5

JSON Console

• Interactive Browser Consoles

– Chrome :

• ctrl+shift+j (cmd+shift+j for mac)

• Select Console tab

– Firefox

• About:blank in address bar for blank tab

• Tools->Web Developer->Web Console (ctrl+shift+k)

– IE

• F12

• Select console tab

• Other

– Firefox tool Scratchpad

– Jsbin.com

– Chrome extension JavaScript Editor

JSON and JavaScript

• Creating objects in Javascript – Empty object:

• var sampleObj = {};

– New object • var sampleObj = new Object();

– Initialized object: • var sampleObj = {“firstname”:”rich”,”age”:50};

– Array object: var sampleArray = { “directory” : [ {“firstname”:”rich”,”age”:35}, {“firstname”:”bob”,”age”:50}, {“firstname”:”emma”,”age”:12} ] };

Page 6: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

6

JSON and JavaScript

• Accessing JSON object fields var x = sampleObj.firstname ; // x = “rich”

var y = sampleObj.age; // y = 50

• Accessing JSON array object fields var x = sampleArray.directory[2].firstname; // x = “emma”

var y = sampleArray.directory[0].age; // y = 35

var z = sampleArray.directory[1];

// z = {firstname:”bob”, age:50}

Principles of Enterprise Web Development

Packaging Data for the Web

EN 605.681

Page 7: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

7

JSON and Java

• Official standard API, JSR 353 – Java API for JSON Processing

– https://jcp.org/en/jsr/detail?id=353

• Several open source APIs existed prior to JSR 353 and are quite popular today – FasterXML/Jackson: https://github.com/FasterXML

– GSON: https://github.com/google/gson (Google)

– JSON-P: https://javaee.github.io/jsonp/

– JSON.org: http://www.json.org

– MOXy: https://wiki.eclipse.org/EclipseLink/FAQ/MOXy

FasterXML/Jackson Overview

• Streaming API that parses and generates JSON in a series of events – Low overhead – Fast read/writes

• Tree model for loading and manipulating JSON document as Java objects (JsonNodes) – Most flexible for manipulation

• Data binding converts JSON to plain old Java objects (POJOs) using configurations or annotations and generates JSON from configured POJOs – Useful for serializing/deserializing Java Objects – Especially for web based application!

• Full documentation with tutorials and more at: https://github.com/FasterXML/jackson-docs

Page 8: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

8

FasterXML/Jackson Databinding • Getting and Installing

– Download from https://github.com/FasterXML • URLs provided to included in Maven builds • Links to jar files with library files, source code and api docs

– Three library jar files required for minimal access • jackson-core • jackson-databind • jackson-annotations

• API Usage – com.fasterxml.jackson.databind.ObjectMapper

• Primary class to facilitate the marshalling and unmarshalling of java objects to and from JSON

• Constructor and supporting methods

– com.fasterxml.jackson.core package • core classes used by databind package (aka ObjectMapper) • Exception classes thrown by class methods throughout Jackson

– com.fasterxml.jackson.annotations package • All supported annotations used for java class mapping to JSON formats • Helper classes that are used by ObjectMapper

ObjectMapper

• Provides function of converting Java objects to JSON and vice versa

– Wraps other Jackson objects (from core library) for reading/writing JSON

• import com.fasterxml.jackson.databind.ObjectMapper • Constructor: new ObjectMapper() • Methods:

– readValue: several methods for moving data sources (streams, files, Strings) into Java objects • readValue(String content, Class<T> valueType) : deserializes JSON content into object of type

valueType

– writeValue: several methods for serializing Java objects into JSON output into Strings, files, streams

• writeValueAsString : serializes Java object to String

– enable: methods for configuring the mapper for parsing JSON or generating JSON

• Exceptions – IOException – JsonGenerationException – JsonMappingException – JsonProcessingException – JsonParseException

Page 9: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

9

Annotations

• Meta data to control how Java classes map to JSON documents and vice versa

• Added to either property or one of the accessor/mutator methods on that property

• Some have additional properties that can be set • @JsonProperty

– value: provides a name of the property in the JSON or just use name of java filed

– defaultValue: value for the property if one is not provided

• @JsonRootName – value: root name to use around object if root-level wrapping is

enabled

• @JsonIgnore • @JsonInclude • @JsonPropertyOrder

Principles of Enterprise Web Development

Packaging Data for the Web

EN 605.681

Page 10: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

10

Using JSON with ReST

• The JAX-RS API (aka Jersey) interfaces with the JAX-B (JSON/XML binding) API inherently when necessary – JSON passed in conjunction with appropriate @Consumes annotation

will use JAX-B to deserialize JSON into objects.

– Java objects passed in conjunction with appropriate @Produces annotation will use JAX-B to serialize objects into JSON.

• Jersey projects must have a JAX-B implementation to support those API calls or they will fail – Can use default JAX-B API implementation packaged with Jersey

– Will replace default with alternative JAX-B implementations through library manipulation and code configuration

FasterXML/Jackson with ReST • FasterXML/Jackson libraries are required for processing the

underlying POJOs and JSON mappings – jackson-core – jackson-databind – jackson-annotations

• Need additional libraries that account for Jackson under the Java API for RESTful Web Services (JAX-RS) – jackson-module-jaxb-annotations – jersey-media-json-jackson

• Out of the box, Jersey provides another JAXB API. To use another, appropriate jar files must be provided for the other implementation and it must be added as a resource so it is recognized by Jersey at runtime. – For jackson, this requires the support of on more jar

• jersey-entity-filtering

Page 11: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

11

Eclipse Project View

Libraries provided from Jersey JAX-RS Download Bundle

Additional libraries necessary for integration

FasterXML/Jackson libraries for use under JAX-RS

Configure Jersey for Jackson

Must add JacksonFeature class to recognized resources in Jersey during configuration

import org.glassfish.jersey.jackson.JacksonFeature;

@ApplicationPath("webresources")

public class ApplicationConfig extends Application {

@Override

public Set<Class<?>> getClasses() {

Set<Class<?>> resources = new HashSet<>();

addRestResourceClasses(resources);

return resources;

}

Private void addRestResourceClasses(Set<Class<?>> resources) {

resources.add(edu.rfs.Endpoints.class);

:

resources.add(JacksonFeature.class);

}

}

Page 12: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

12

Using JSON Binding in ReST

• Create POJOs that will be mapped to desired JSON (and vice-versa) – Objects create from JSON passed from client

– JSON serialized from objects used on server

• Optionally, employ Jackson annotations on object to dictate mapping features (field names, order, etc)

• Employ the objects in ReST service methods as parameters or return variables in conjunction with @Produces/@Consumes annotations

Example JSON Output

• A ReST call to http://host/path..../record will return content of type application/json that will look as follows: {“first”:”rich”,”last”:”spiegel”,”age”:35}

@GET

@Path(“/record”)

@Produces(MediaType.APPLICATION_JSON)

public Record getRecord() {

Record r = new Record();

r.setFirstname(“Rich”);

r.setLastname(“Spiegel”);

r.setAge(35);

return r;

}

public class Record {

@JsonProperty(“first”)

private String firstname;

@JsonProperty(“last”)

private String lastname;

@JsonProperty(“age”)

private int age;

:

}

Page 13: Data and the Webspiegel/en605681/WebData/... · • XML allows arbitrary definition and use of tags and attributes • Well-formed for to faciliate parsing, search and manipulation

8/10/2019

13

Example JSON Input

• A POST to http://host/path..../new with attached JSON content will automatically create record and initialize it with fields from the JSON passed.

@POST

@Path(“/new”)

@Consumes(MediaType.APPLICATION_JSON)

public Response newRecord(Record record)

{

// record is initialized from the

// json content provided in the POST

// request

}

public class Record {

@JsonProperty(“first”)

private String firstname;

@JsonProperty(“last”)

private String lastname;

@JsonProperty(“age”)

private int age;

:

}

Principles of Enterprise Web Development

Packaging Data for the Web

EN 605.481