Configuring JSON-P in Liberty

You can configure your Liberty environment to use JSON-P by enabling the default jsonp-1.0 feature or by implementing the jsonpContainer-1.0 feature.

About this task

Open Liberty In version 21.0.0.1 and later, for information about using JSON-P with Liberty, see the Open Liberty website.

JSON Processing (JSON-P) 1.1 for WebSphere® Application Server Liberty is built on the JSON-P reference implementation.

Glassfish JSON is the reference implementation of the JSON-P 1.1 specification, and is the JSON-P implementation that Liberty provides by default when the jsonp-1.1 feature is enabled. It is possible to configure an alternative implementation of JSON-P through the jsonpContainer-1.1 feature.

Procedure

  • Select the correct JSON-P feature in Liberty.
    The following features that are offered for JSON-P 1.1 integration with Liberty:
    • jsonp-1.1
    • jsonpContainer-1.0

    The default JSON-P feature is jsonp-1.1, which provides generic JSON-P support and can use any specification-compliant implementation. This feature provides the JSON-P API to applications and features that depend on it. Unless specified otherwise by other features, this feature uses the Glassfish JSON reference implementation that is included in Liberty.

    The jsonpContainer-1.1 feature provides the JSON-P API and provides JSON-P support by using a user supplied JSON-P implementation that is configured as a BELL (Basic Extension using Liberty Libraries). The <bell> server configuration element is used to configure this implementation as a BELL.

    For example, Apache Johnzon is another JSON-P compliant implementation. In this example, a server is configured to use Johnzon instead of Glassfish:

    <server>
      <featureManager>
        <feature>jsonpContainer-1.0</feature>
      </featureManager>
    
      <bell libraryRef="johnzon"/>
    
      <library id="johnzon">
        <fileset dir="${server.config.dir}/johnzon"includes="*.jar"/>
      </library>
    </server>
  • Write features that depend on JSON-P.

    When you write a feature that depends on JSON-P, special design considerations must be made:

    Instead of obtaining a javax.json.spi.JsonProvider instance by using standard mechanisms, such as JsonProvider.provider(), features declare OSGi service dependencies on JsonpProvider, and use these injected services when building objects. The following code is an example:
    @Component( ... )  
    public class ServiceThatRequiresJsonp {
    
      @Reference
      private JsonProvider jsonpProvider;
    
      public void doSomethingWithJsonp() {
        JsonObject o = jsonpProvider.createObjectBuilder().build();
        // ...
      }
    }