Skip to content

Commit 9368442

Browse files
authored
Fix helidon-io#329 Use JsonBuilderFactory instead of Json.create (helidon-io#330)
* Fix helidon-io#329 Use JsonBuilderFactory instead of Json.create()
1 parent 51cae17 commit 9368442

29 files changed

Lines changed: 197 additions & 91 deletions

File tree

docs/src/main/docs/webserver/08_json-support.adoc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@ Routing.builder()
5656
[source,java]
5757
.Handler that receives and returns JSON objects
5858
----
59-
private void sayHello(ServerRequest req, ServerResponse res, JsonObject json) { // <1>
60-
JsonObject msg = Json.createObjectBuilder() // <2>
59+
private static final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Collections.emptyMap()); <1>
60+
61+
private void sayHello(ServerRequest req, ServerResponse res, JsonObject json) { // <2>
62+
JsonObject msg = jsonFactory.createObjectBuilder() // <3>
6163
.add("message", "Hello " + json.getString("name"))
6264
.build();
63-
res.send(msg); // <3>
65+
res.send(msg); // <4>
6466
}
6567
----
66-
<1> JsonObject is passed to handler
67-
<2> Create a JsonObject using JSON-P to hold return data
68-
<3> Send JsonObject in response
68+
<1> Using a `JsonBuilderFactory` is more efficient than `Json.createObjectBuilder()`
69+
<2> JsonObject is passed to handler
70+
<3> Create a JsonObject using JSON-P to hold return data
71+
<4> Send JsonObject in response
6972
7073
[source,bash]
7174
.Example of posting JSON to sayHello endpoint

examples/guides/mp-restful-webservice/src/main/java/io/helidon/guides/mp/restfulwebservice/GreetResource.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
1616

1717
package io.helidon.guides.mp.restfulwebservice;
1818

19+
import java.util.Collections;
20+
1921
// tag::javaxImports[]
2022
import javax.enterprise.context.RequestScoped;
2123
import javax.inject.Inject;
2224
import javax.json.Json;
25+
import javax.json.JsonBuilderFactory;
2326
import javax.json.JsonObject;
2427
import javax.ws.rs.GET;
2528
import javax.ws.rs.PUT;
@@ -53,6 +56,9 @@
5356
@RequestScoped
5457
public class GreetResource {
5558
// end::classDecl[]
59+
60+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
61+
5662
/**
5763
* The greeting message provider.
5864
*/
@@ -128,7 +134,7 @@ public JsonObject getMessage(@PathParam("name") String name) { // <4>
128134
public JsonObject updateGreeting(@PathParam("greeting") String newGreeting) { // <4>
129135
greeting.setMessage(newGreeting);
130136

131-
return Json.createObjectBuilder()
137+
return JSON.createObjectBuilder()
132138
.add("greeting", newGreeting)
133139
.build();
134140
}
@@ -138,7 +144,7 @@ public JsonObject updateGreeting(@PathParam("greeting") String newGreeting) { //
138144
private JsonObject createResponse(String who) { // <1>
139145
String msg = String.format("%s %s!", greeting.getMessage(), who); // <2>
140146

141-
return Json.createObjectBuilder() // <3>
147+
return JSON.createObjectBuilder() // <3>
142148
.add("message", msg)
143149
.build();
144150
}

examples/guides/mp-restful-webservice/src/main/java/io/helidon/guides/mp/restfulwebservice/HealthResource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,13 @@
1515
*/
1616
package io.helidon.guides.mp.restfulwebservice;
1717

18+
import java.util.Collections;
19+
1820
// tag::javaxImports[]
1921
import javax.enterprise.context.RequestScoped;
2022
import javax.inject.Inject;
2123
import javax.json.Json;
24+
import javax.json.JsonBuilderFactory;
2225
import javax.json.JsonObject;
2326
import javax.ws.rs.GET;
2427
import javax.ws.rs.Path;
@@ -35,6 +38,8 @@
3538
public class HealthResource {
3639
// end::classDecl[]
3740

41+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
42+
3843
// tag::greetingDecl[]
3944
@Inject // <1>
4045
private GreetingMessage greeting; // <2>
@@ -56,7 +61,7 @@ public Response alive() {
5661
if (greetResourceError == null) { // <4>
5762
response = Response.ok().build();
5863
} else {
59-
JsonObject returnObject = Json.createObjectBuilder()
64+
JsonObject returnObject = JSON.createObjectBuilder()
6065
.add("error", greetResourceError)
6166
.build();
6267
response = Response

examples/guides/se-restful-webservice/src/main/java-start/io/helidon/guides/se/restfulwebservice/GreetService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616

1717
package io.helidon.guides.se.restfulwebservice;
1818

19+
import java.util.Collections;
20+
1921
import javax.json.Json;
22+
import javax.json.JsonBuilderFactory;
2023
import javax.json.JsonObject;
2124

2225
import io.helidon.config.Config;
@@ -48,6 +51,8 @@ public class GreetService implements Service {
4851
*/
4952
private static final Config CONFIG = Config.create().get("app");
5053

54+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
55+
5156
/**
5257
* The config value for the key {@code greeting}.
5358
*/
@@ -74,7 +79,7 @@ private void getDefaultMessage(final ServerRequest request,
7479
final ServerResponse response) {
7580
String msg = String.format("%s %s!", greeting, "World");
7681

77-
JsonObject returnObject = Json.createObjectBuilder()
82+
JsonObject returnObject = JSON.createObjectBuilder()
7883
.add("message", msg)
7984
.build();
8085
response.send(returnObject);
@@ -90,7 +95,7 @@ private void getMessage(final ServerRequest request,
9095
String name = request.path().param("name");
9196
String msg = String.format("%s %s!", greeting, name);
9297

93-
JsonObject returnObject = Json.createObjectBuilder()
98+
JsonObject returnObject = JSON.createObjectBuilder()
9499
.add("message", msg)
95100
.build();
96101
response.send(returnObject);
@@ -105,7 +110,7 @@ private void updateGreeting(final ServerRequest request,
105110
final ServerResponse response) {
106111
greeting = request.path().param("greeting");
107112

108-
JsonObject returnObject = Json.createObjectBuilder()
113+
JsonObject returnObject = JSON.createObjectBuilder()
109114
.add("greeting", greeting)
110115
.build();
111116
response.send(returnObject);

examples/guides/se-restful-webservice/src/main/java/io/helidon/guides/se/restfulwebservice/GreetService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,11 @@
1616

1717
package io.helidon.guides.se.restfulwebservice;
1818

19+
import java.util.Collections;
20+
1921
//tag::importsStart[]
2022
import javax.json.Json;
23+
import javax.json.JsonBuilderFactory;
2124
import javax.json.JsonObject;
2225

2326
import io.helidon.config.Config;
@@ -61,6 +64,8 @@ public class GreetService implements Service {
6164
private static final Config CONFIG = Config.create().get("app"); // <1>
6265
// end::CONFIG[]
6366

67+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
68+
6469
/**
6570
* The config value for the key {@code greeting}.
6671
*/
@@ -112,7 +117,7 @@ private void getDefaultMessage(final ServerRequest request,
112117
final ServerResponse response) {
113118
String msg = String.format("%s %s!", greeting, "World"); // <1>
114119

115-
JsonObject returnObject = Json.createObjectBuilder()
120+
JsonObject returnObject = JSON.createObjectBuilder()
116121
.add("message", msg) // <2>
117122
.build();
118123
response.send(returnObject); // <3>
@@ -130,7 +135,7 @@ private void getMessage(final ServerRequest request,
130135
String name = request.path().param("name"); // <1>
131136
String msg = String.format("%s %s!", greeting, name);
132137

133-
JsonObject returnObject = Json.createObjectBuilder()
138+
JsonObject returnObject = JSON.createObjectBuilder()
134139
.add("message", msg)
135140
.build();
136141
response.send(returnObject);
@@ -147,7 +152,7 @@ private void updateGreeting(final ServerRequest request,
147152
final ServerResponse response) {
148153
greeting = request.path().param("greeting"); // <1>
149154

150-
JsonObject returnObject = Json.createObjectBuilder() // <2>
155+
JsonObject returnObject = JSON.createObjectBuilder() // <2>
151156
.add("greeting", greeting)
152157
.build();
153158
response.send(returnObject);

examples/guides/se-restful-webservice/src/main/java/io/helidon/guides/se/restfulwebservice/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.helidon.guides.se.restfulwebservice;
1818

1919
import java.io.IOException;
20+
import java.util.Collections;
2021
import java.util.logging.LogManager;
2122

2223
// tag::importsHealth1[]
@@ -42,6 +43,7 @@
4243
// end::importsEnd[]
4344
// tag::importsHealth3[]
4445
import javax.json.Json;
46+
import javax.json.JsonBuilderFactory;
4547
import javax.json.JsonObject;
4648
// end::importsHealth3[]
4749

@@ -54,6 +56,8 @@ public final class Main {
5456
private static GreetService greetService;
5557
// end::greetServiceDecl[]
5658

59+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
60+
5761
/**
5862
* Cannot be instantiated.
5963
*/
@@ -156,7 +160,7 @@ private static void alive(final ServerRequest request,
156160
.status(Http.Status.OK_200) //<2>
157161
.send();
158162
} else {
159-
JsonObject returnObject = Json.createObjectBuilder() //<3>
163+
JsonObject returnObject = JSON.createObjectBuilder() //<3>
160164
.add("error", greetServiceError)
161165
.build();
162166
response

examples/guides/se-restful-webservice/src/test/java/io/helidon/guides/se/restfulwebservice/MainTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,15 @@
1616

1717
package io.helidon.guides.se.restfulwebservice;
1818

19+
import java.util.Collections;
1920
import java.util.concurrent.TimeUnit;
2021
import java.net.URL;
2122
import java.net.HttpURLConnection;
2223

2324
import javax.json.Json;
2425
import javax.json.JsonObject;
2526
import javax.json.JsonReader;
27+
import javax.json.JsonReaderFactory;
2628

2729
import io.helidon.webserver.WebServer;
2830

@@ -34,6 +36,7 @@
3436
public class MainTest {
3537

3638
private static WebServer webServer;
39+
private static final JsonReaderFactory JSON = Json.createReaderFactory(Collections.emptyMap());
3740

3841
@BeforeAll
3942
public static void startTheServer() throws Exception {
@@ -58,14 +61,14 @@ public void testHelloWorld() throws Exception {
5861

5962
conn = getURLConnection("GET","/greet");
6063
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response1");
61-
JsonReader jsonReader = Json.createReader(conn.getInputStream());
64+
JsonReader jsonReader = JSON.createReader(conn.getInputStream());
6265
JsonObject jsonObject = jsonReader.readObject();
6366
Assertions.assertEquals("Hello World!", jsonObject.getString("message"),
6467
"default message");
6568

6669
conn = getURLConnection("GET", "/greet/Joe");
6770
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response2");
68-
jsonReader = Json.createReader(conn.getInputStream());
71+
jsonReader = JSON.createReader(conn.getInputStream());
6972
jsonObject = jsonReader.readObject();
7073
Assertions.assertEquals("Hello Joe!", jsonObject.getString("message"),
7174
"hello Joe message");
@@ -74,7 +77,7 @@ public void testHelloWorld() throws Exception {
7477
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response3");
7578
conn = getURLConnection("GET", "/greet/Jose");
7679
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response4");
77-
jsonReader = Json.createReader(conn.getInputStream());
80+
jsonReader = JSON.createReader(conn.getInputStream());
7881
jsonObject = jsonReader.readObject();
7982
Assertions.assertEquals("Hola Jose!", jsonObject.getString("message"),
8083
"hola Jose message");

examples/microprofile/hello-world-explicit/src/main/java/io/helidon/microprofile/example/helloworld/explicit/HelloWorldResource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@
1717
package io.helidon.microprofile.example.helloworld.explicit;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
2021

2122
import javax.enterprise.context.RequestScoped;
2223
import javax.enterprise.inject.spi.BeanManager;
2324
import javax.inject.Inject;
2425
import javax.json.Json;
26+
import javax.json.JsonBuilderFactory;
2527
import javax.json.JsonObject;
2628
import javax.ws.rs.GET;
2729
import javax.ws.rs.Path;
@@ -39,6 +41,7 @@
3941
@Path("helloworld")
4042
@RequestScoped
4143
public class HelloWorldResource {
44+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
4245
private final Config config;
4346
private final String applicationName;
4447
private final URI applicationUri;
@@ -84,7 +87,7 @@ public String message() {
8487
@GET
8588
@Produces(MediaType.APPLICATION_JSON)
8689
public JsonObject getHello(@PathParam("name") String name) {
87-
return Json.createObjectBuilder()
90+
return JSON.createObjectBuilder()
8891
.add("name", name)
8992
.add("appName", applicationName)
9093
.add("appUri", String.valueOf(applicationUri))

examples/microprofile/hello-world-implicit/src/main/java/io/helidon/microprofile/example/helloworld/implicit/HelloWorldResource.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,14 @@
1717
package io.helidon.microprofile.example.helloworld.implicit;
1818

1919
import java.net.URI;
20+
import java.util.Collections;
2021
import java.util.logging.Logger;
2122

2223
import javax.enterprise.context.RequestScoped;
2324
import javax.enterprise.inject.spi.BeanManager;
2425
import javax.inject.Inject;
2526
import javax.json.Json;
27+
import javax.json.JsonBuilderFactory;
2628
import javax.json.JsonObject;
2729
import javax.ws.rs.GET;
2830
import javax.ws.rs.Path;
@@ -43,6 +45,9 @@
4345
@Path("helloworld")
4446
@RequestScoped
4547
public class HelloWorldResource {
48+
49+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
50+
4651
private final Config config;
4752
private final Logger logger;
4853
private final int requestId;
@@ -96,7 +101,7 @@ public String message() {
96101
@GET
97102
@Produces(MediaType.APPLICATION_JSON)
98103
public JsonObject getHello(@PathParam("name") String name) {
99-
return Json.createObjectBuilder()
104+
return JSON.createObjectBuilder()
100105
.add("name", name)
101106
.add("requestId", requestId)
102107
.add("appName", applicationName)

0 commit comments

Comments
 (0)