Files
helidon/docs/se/webserver/08_json-support.adoc
Romain Grecourt 90ee65f33c Fix h1Prefix to SE and MP for all pages under /se and /mp respectively. (#2074)
Fix various links and formatting issues across the docs.
2020-06-19 14:25:55 -07:00

93 lines
3.1 KiB
Plaintext

///////////////////////////////////////////////////////////////////////////////
Copyright (c) 2018, 2020 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
///////////////////////////////////////////////////////////////////////////////
= JSON Support
:h1Prefix: SE
:description: Helidon Reactive WebServer JSON support
:keywords: helidon, reactive, reactive streams, reactive java, reactive webserver
== Json Support
The WebServer supports JSON-P. When enabled, you can send and
receive JSON-P objects transparently.
=== Maven Coordinates
Declare the following dependency in your project:
[source,xml,subs="verbatim,attributes"]
.Webserver JSON-P Dependency
----
<dependency>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-jsonp</artifactId>
</dependency>
----
=== Usage
To enable JSON-P support, first register it with the web server.
Then you can add routes that handle and return JSON.
[source,java]
.Configure JsonpSupport and use it for reading and writing of entities
----
JsonpSupport jsonbSupport = JsonpSupport.create(); // <1>
WebServer webServer = WebServer.builder()
.addMediaSupport(jsonpSupport) // <2>
.build();
----
<1> Register JsonpSupport to enable transformation from and to `JsonObject` objects
<2> Register that JsonpSupport instance to enable automatic
deserialization of Java objects from and serialization of Java objects
to JSON.
[source,java]
.Handler that receives and returns JSON objects
----
private static final JsonBuilderFactory JSON_FACTORY = Json.createBuilderFactory(Collections.emptyMap()); <1>
private void sayHello(ServerRequest req, ServerResponse res, JsonObject json) { // <2>
JsonObject msg = JSON_FACTORY.createObjectBuilder() // <3>
.add("message", "Hello " + json.getString("name"))
.build();
res.send(msg); // <4>
}
----
<1> Using a `JsonBuilderFactory` is more efficient than `Json.createObjectBuilder()`
<2> JsonObject is passed to handler
<3> Create a JsonObject using JSON-P to hold return data
<4> Send JsonObject in response
[source,bash]
.Example of posting JSON to sayHello endpoint
----
curl --noproxy '*' -X POST -H "Content-Type: application/json" \
http://localhost:8080/sayhello -d '{"name":"Joe"}'
{"message":"Hello Joe"}
----
=== Configuring Json Reader/Writer factories
To configure JSON-P `JsonReaderFactory` and `JsonWriterFactory` that are used by
the `JsonpSupport` instance, create the `JsonpSupport` object:
[source,java]
.Create `JsonpSupport` with the provided configuration
----
JsonpSupport.create(Map.of(JsonGenerator.PRETTY_PRINTING, false))
----