DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Languages
  4. Client Side MOXy JSON Binding Explained

Client Side MOXy JSON Binding Explained

Blaise Doughan user avatar by
Blaise Doughan
·
Mar. 19, 12 · Interview
Like (0)
Save
Tweet
Share
12.38K Views

Join the DZone community and get the full member experience.

Join For Free
Recently I posted how to leverage EclipseLink JAXB (MOXy)'s JSON binding to create a RESTful service.  In this post I will demonstrate how easy it is to take advantage of MOXy's JSON binding on the client side.
  • MOXy as Your JAX-RS JSON Provider - Server Side
  • MOXy as Your JAX-RS JSON Provider - Client Side

URI

This post will focus on the following URI from the service we declared in the previous post.  The following call will return a list of customers that live in "Any Town".

1
http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town

Java SE Client APIs

In the first example we will use the standard Java SE 6 APIs.  Some interesting items to note:
  • MOXy can directly marshal (line 35) and unmarshal (line 28) collections to/from JSON arrays without requiring a wrapper object.
  • There are no compile time dependencies on MOXy (it is a run time dependency).
  • The eclipselink.media-type property is used to enable JSON binding on the unmarshaller (line 25) and marshaller (line 33).
  • The eclipselink.json.include-root property is used to indicate that the @XmlRootElement annotation should be ignored in the JSON binding (lines 26 and 34).

package example;
 
import java.io.InputStream;
import java.net.*;
import java.util.List;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.example.Customer;
 
public class JavaSEClient {
 
    private static final String MEDIA_TYPE = "application/json";
 
    public static void main(String[] args) throws Exception {
        String uri = "http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town";
        URL url = new URL(uri);
        HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", MEDIA_TYPE);
 
        JAXBContext jc = JAXBContext.newInstance(Customer.class);
 
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        InputStream xml = connection.getInputStream();
        List<Customer> customers = (List<Customer>) unmarshaller.unmarshal(new StreamSource(xml), Customer.class).getValue();
        connection.disconnect();
 
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.marshal(customers, System.out);
    }
 
}

Output

Below is the output from running the Java SE client.  For those that may have used a JAXB (JSR-222) implementation with something like Jettison to produce/consume JSON, the following are some interesting items to note:
  • MOXy renders collections as JSON arrays.
  • MOXy represents the numeric values correctly without quotes (line 26).
  • MOXy surrounds collections of size 1 correctly with square brackets (lines 28 and 32).

[ {
   "address" : {
      "city" : "Any Town",
      "id" : 1,
      "street" : "1 A Street"
   },
   "firstName" : "Jane",
   "id" : 1,
   "lastName" : "Doe",
   "phoneNumbers" : [ {
      "id" : 2,
      "num" : "555-2222",
      "type" : "HOME"
   }, {
      "id" : 1,
      "num" : "555-1111",
      "type" : "WORK"
   } ]
}, {
   "address" : {
      "city" : "Any Town",
      "id" : 10,
      "street" : "456 Another Road"
   },
   "firstName" : "Sue",
   "id" : 10,
   "lastName" : "Jones",
   "phoneNumbers" : [ {
      "id" : 10,
      "num" : "555-3333",
      "type" : "WORK"
   } ]
} ]
Jersey Client APIs

JAX-RS 2.0 (JSR-339) is working on standardizing the client APIs.  With JAX-RS 1.0 many of the implementations provide their own version.  Below is an example using the client APIs provided by Jersey.  Note how we can leverage the exact same MessageBodyReader/Writer that we used on the server side (line 14, refer to MOXy as Your JAX-RS JSON Provider - Server Side).  I have also specified the LoggingFilter (line 17) so we can take a closer look at the message.
package example;
 
import java.util.List;
import org.example.Customer;
import org.example.MOXyJSONProvider;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.*;
import com.sun.jersey.api.client.filter.LoggingFilter;
 
public class JerseyClient {
 
    public static void main(String[] args) {
        ClientConfig cc = new DefaultClientConfig();
        cc.getClasses().add(MOXyJSONProvider.class);
 
        Client client = Client.create(cc);
        client.addFilter(new LoggingFilter());
 
        WebResource resource = client.resource("http://localhost:8080/CustomerService/rest/customers");
        List<Customer> customers = resource.path("/findCustomersByCity/Any%20Town").accept("application/json").get(new GenericType<List<Customer>>(){});
 
        for(Customer customer : customers) {
            System.out.println(customer.getFirstName());
        }
    }
 
}


Output

Below is the output from running the Jersey client.
14-Mar-2012 4:08:12 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > GET http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town
1 > Accept: application/json
1 >
 
14-Mar-2012 4:08:12 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 200
1 < Transfer-Encoding: chunked
1 < Date: Wed, 14 Mar 2012 20:08:12 GMT
1 < Content-Type: application/json
1 < X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Oracle Corporation/1.7)
1 < Server: GlassFish Server Open Source Edition 3.1.1
1 <
[{"address" : {"city" : "Any Town", "id" : 1, "street" : "1 A Street"}, "firstName" : "Jane", "id" : 1, "lastName" : "Doe", "phoneNumbers" : [{"id" : 1, "num" : "555-1111", "type" : "WORK"}, {"id" : 2, "num" : "555-2222", "type" : "HOME"}]}, {"address" : {"city" : "Any Town", "id" : 10, "street" : "456 Another Road"}, "firstName" : "Sue", "id" : 10, "lastName" : "Jones", "phoneNumbers" : [{"id" : 10, "num" : "555-3333", "type" : "WORK"}]}]
 
Doe, Jane
Jones, Sue

JSON Binding (linguistics)

Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Code Review Solution
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • The Power of Zero-Knowledge Proofs: Exploring the New ConsenSys zkEVM

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: