Using the Jackson JSON processor and the Apache Wink: simpler Jax-rs and Ajax integration

Source: Internet
Author: User
Tags json serialization

The Apache Wink is rapidly growing into one of the standard implementations of the JAX-RS 1.0 specification. The providers for the JSON grouping and reconciliation group included with the Apache Wink version, such as json.org and jettison, have some problems with array representation, and the return type is relatively limited. It is difficult to write Jax-rs services and their customers asynchronous JavaScript and XML (Ajax) applications. In this article, learn how to configure an existing WEB application that supports Apache Wink, and use the Jackson JSON provider to solve some of the problems. The benefits of this provider will be demonstrated using a simple code example that supports the Jackson-jax-rs Web service.

Brief introduction

The Apache Wink is rapidly growing into one of the standard implementations of the JAX-RS 1.0 specification. Json.org and jettison are the default providers for JSON synchronization, but they themselves have certain problems. For example, their array representations and limited return types make it difficult to write Jax-rs services and their customers ' Ajax applications.

This article outlines a way to configure an existing WEB application that supports Apache Wink to use the Jackson JSON provider. Understand the benefits of this provider by using a simple sample code that supports the Jackson-jax-rs Web service.

JSON provider included with Apache Wink

Apache Wink packs Two JSON providers as part of the standard version: Json.org and jettison implementations. Both providers complicate the integration of the Wink service and the Ajaxboth application. Neither provider can serialize a Java list directly to JSON as a return type, because JAXB requires an encapsulated XML element. There are a number of other problems with the two, such as:

json.org

Array serialization of json.org providers is predictable, but this behavior is undesirable when interacting with Ajax. When it shows arrays of different sizes, json.org renders them in different ways:

2 +: "correct" array serialization. Example: object: {array: ["Element1", "Element2"]}

1: Collapses the array. Example: object: {array: "Element1"}

0: Remove the array completely. Example: Object: {}

Clearly, coding restrictions on different structures in Javascript can lead to unnecessary, unnecessary complexity.

Jettison

Jettison uses badgerfish conventions for JSON generation, which generates a structure that is difficult to navigate after converting to a Javascript object.

Jackson

Jackson is essentially a JSON processor for generating and parsing the JSON representation of Java objects. Jackson can also be configured as a JSON serialization provider for JAX-RS implementations.

As a jax-rs JSON serialization provider, as described below, Jackson has several advantages that json.org and jettison do not have.

Table 1. Jackson's Advantages

Advantages Description
The ability to serialize a list locally Jackson can return a list of string objects directly from the service without the need for an encapsulated XML element.
Array processing Jackson has a good, predictable array of serialization facilities.
Speed Has a faster speed than other providers.
License The Apache License 2.0 is well understood. Components that use this license are used for commercial and free software products.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.