Thursday, January 14, 2016

Web Services With JSON and XML

XML and JSON are the most popular formats for exchanging data over the Internet.  JSON has become more popular recently because it's less verbose than XML.  For example, this XML:
<geonames>
    <country>
        <countrycode>AD</countryCode>
        <countryname>Andorra</countryName>
    </country>
    <country>
        <countrycode>AR</countryCode>
        <countryname>Argentina</countryName>
    </country>
</geonames>
Looks like this in JSON:
{"geonames": [
    {
        "countryCode": "AD",
        "countryName": "Andorra"
    },
    {
        "countryCode": "AR",
        "countryName": "Argentina"
    }
]}
JSON uses fewer characters, takes up less space and is easier to read.

Because JSON and XML representations of data can be used almost interchangeably, many web services are offered in both versions for convenience.  Here are a couple showing postal codes in London's W1J postal area:

XML:
http://api.geonames.org/postalCodeSearch?postalcode=W1J&maxRows=2&username=demo

JSON:
http://api.geonames.org/postalCodeSearchJSON?postalcode=W1J&maxRows=2&username=demo

They have slightly different URL endpoints, postalCodeSearch and postalCodeSearchJSON, and they return the same data, just in different formats.

Using Java to Produce and Consume RESTful Web Services


Java is one of the oldest and most popular languages for web development, especially for applications deployed on web servers. It includes packages to simplify producing and consuming RESTful web services. In addition, there are many popular Java community-built APIs for web services. Some of the APIs are so popular that they have been incorporated into the Java JDK.

JAXB - Java Architecture for XML Binding


Java objects can be serialized into an XML string using JAXB. Likewise, an XML string that represents a Java object can deserialized into a Java object. Serializing is called "marshalling" and deserializing is called "unmarshalling". Java classes to be marshalled into XML are marked up using annotations from the javax.xml.bind.annotation.* namespace, for example, @XmlRootElement and @XmlElement.

JAXB includes a couple tools to simplify creating annotated Java classes or XML schemas:

xjc - creates annotated Java classes from an XML schema (XSD file)

schemagen - creates an XML schema from annotated Java classes

See here for examples and usage.

(show example Java class, XML and XML schema)

JAX-RS - Java API for RESTful Web Services


JAX-RS is the Java API for RESTful Web Services that simplifies creating and consuming web services using XML and JSON.  There are several popular implementations:
I'll talk about Jersey because it's the one I know best.  I use Jersey 1.19, the last version of Jersey before version 2. Version 2 has features I don't find useful.

Jersey uses a reference implementation (RI) of JAXB version 2.x or higher to serialize Java objects to XML and deserialize XML to Java objects.

Since JAXB 2.x or higher is included in the Java JDK starting with Java 6, no additional dependency is required if you're using Java 6, 7, 8 or higher.  Here are the versions of JAXB included with Java 6 and 7.

To serialize the JAXB beans (Java classes) to JSON and deserialize JSON to JAXB beans when using the MIME media type application/json, you do need to include an additional dependency. With Maven, it looks like this:
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19</version>
</dependency>
See 11.4.1.1. JAXB RI (reference implementation) for more.

Jersey XML Support

Jersey JSON Support





No comments:

Post a Comment

DBeaver vs. SQL Developer: DBeaver fail!

DBeaver claims to be the best database editor. Thanks to some bad UI, something as simple as switching the database schema in the SQL editor...