Class JsonSupport

  • All Implemented Interfaces:
    Handler, Service, BiConsumer<ServerRequest,​ServerResponse>

    public final class JsonSupport
    extends JsonService
    It provides contains JSON-P (javax.json) support for WebServer's Routing. It is intended to provide readers and writers for javax.json objects such as JsonObject or JsonArray. If registered on the Web Server Routing, then all Handlers can use ServerRequest.content(). as(...) and ServerResponse.send() with JSON objects.

    Get Instance

    Use factory methods create() or create(io.helidon.media.jsonp.common.JsonProcessing) to acquire an instance.

    Usage with Routing

    JsonSupport should be registered on the routing before any business logic handlers.
    
     Routing.builder()
            .register(JsonSupport.create())
            .etc.... // Business logic related handlers
     
    Instance behaves also as a routing filter. It means that it can be registered on any routing rule (for example HTTP method) and then it can be used in following handlers with compatible rules.
    
     // Register JsonSupport only for POST of 'foo'
     Routing.builder()
            .post("/foo/{}", JsonSupport.create())
            .post("/foo/bar", ...) // It can use JSON structures
            .get("/foo/bar", ...);  // It can NOT use JSON structures
     
    See Also:
    Routing, JsonStructure, JsonReader, JsonWriter
    • Method Detail

      • accept

        public void accept​(ServerRequest request,
                           ServerResponse response)
        It registers reader and writer for JsonSupport on ServerRequest/ServerResponse on provided routing criteria.

        This method is called from Routing during build process. The user should register whole class ot the routing criteria. For example: Routing.builder(). post("/foo", JsonSupport.create()).

        It calls ServerRequest.next() method to invoke following handlers with particular business logic.

        Parameters:
        request - a server request
        response - a server response
        See Also:
        Routing
      • reader

        public Reader<javax.json.JsonStructure> reader​(Charset charset)
        Returns a function (reader) converting Publisher of ByteBuffers to a JSON-P object.

        It is intended for derivation of others, more specific readers.

        Parameters:
        charset - a charset to use or null for default charset
        Returns:
        the byte array content reader that transforms a publisher of byte buffers to a completion stage that might end exceptionally with a IllegalArgumentException in case of I/O error or a JsonException
      • reader

        public Reader<javax.json.JsonStructure> reader()
        Returns a function (reader) converting Publisher of ByteBuffers to a JSON-P object.

        It is intended for derivation of others, more specific readers.

        Returns:
        the byte array content reader that transforms a publisher of byte buffers to a completion stage that might end exceptionally with a IllegalArgumentException in case of I/O error or a JsonException
      • writer

        public Function<javax.json.JsonStructure,​Flow.Publisher<DataChunk>> writer​(Charset charset)
        Returns a function (writer) converting JsonStructure to the Publisher of DataChunks.
        Parameters:
        charset - a charset to use or null for default charset
        Returns:
        created function
      • create

        public static JsonSupport create​(JsonProcessing processing)
        Create a JsonSupport with customized processing configuration.
        Parameters:
        processing - processing to get JSON-P readers and writers
        Returns:
        json support to register with web server
        See Also:
        JsonProcessing.builder()