From e2bd18eadaf398c58eb084113fbf240a1ca51379 Mon Sep 17 00:00:00 2001 From: Thibault Vallin Date: Tue, 5 Sep 2023 20:15:42 +0200 Subject: [PATCH] 3.x: Use JSON-B instead of JSON-P in MP quickstarts (#7487) * Update microprofile-cors * Update micrometer * Update quickstart standalone * Update quickstart * Update microstream * Update metrics filtering * Update status count * Update openapi basic * review changes Signed-off-by: tvallin --- examples/integrations/micrometer/mp/pom.xml | 5 +- .../micrometer/mp/GreetResource.java | 39 +++----- .../micrometer/mp/GreetingMessage.java | 58 ++++++++++++ .../micrometer/mp/TestEndpoint.java | 14 ++- .../microstream/greetings-mp/pom.xml | 5 + .../greetings/mp/GreetResource.java | 38 +++----- .../greetings/mp/GreetingMessage.java | 57 +++++++++++ .../mp/MicrostreamExampleGreetingsMpTest.java | 10 +- examples/metrics/filtering/mp/pom.xml | 7 +- .../metrics/filtering/mp/GreetResource.java | 34 ++----- .../metrics/filtering/mp/GreetingMessage.java | 57 +++++++++++ .../metrics/filtering/mp/MainTest.java | 5 +- examples/microprofile/cors/pom.xml | 5 + .../examples/cors/GreetResource.java | 38 +++----- .../examples/cors/GreetingMessage.java | 58 ++++++++++++ .../microprofile/examples/cors/TestCORS.java | 31 ++---- .../microprofile/http-status-count-mp/pom.xml | 8 -- .../mp/httpstatuscount/GreetResource.java | 23 +++-- .../{Message.java => GreetingMessage.java} | 47 +++------- .../httpstatuscount/SimpleGreetResource.java | 8 +- .../examples/mp/httpstatuscount/MainTest.java | 12 +-- .../examples/openapi/basic/GreetResource.java | 94 +++---------------- .../openapi/basic/GreetingMessage.java | 57 +++++++++++ .../examples/openapi/basic/MainTest.java | 22 ++--- .../quickstarts/helidon-quickstart-mp/pom.xml | 5 + .../examples/quickstart/mp/GreetResource.java | 62 +++--------- .../quickstart/mp/GreetingMessage.java | 57 +++++++++++ .../examples/quickstart/mp/MainTest.java | 23 +++-- .../helidon-standalone-quickstart-mp/pom.xml | 5 + .../examples/quickstart/mp/GreetResource.java | 38 +++----- .../quickstart/mp/GreetingMessage.java | 57 +++++++++++ .../examples/quickstart/mp/MainTest.java | 23 +++-- 32 files changed, 599 insertions(+), 403 deletions(-) create mode 100644 examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetingMessage.java create mode 100644 examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetingMessage.java create mode 100644 examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetingMessage.java create mode 100644 examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetingMessage.java rename examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/{Message.java => GreetingMessage.java} (56%) create mode 100644 examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetingMessage.java create mode 100644 examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java create mode 100644 examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java diff --git a/examples/integrations/micrometer/mp/pom.xml b/examples/integrations/micrometer/mp/pom.xml index de5dc41fde0..e9f421eb772 100644 --- a/examples/integrations/micrometer/mp/pom.xml +++ b/examples/integrations/micrometer/mp/pom.xml @@ -50,8 +50,9 @@ microprofile-openapi-api - io.helidon.media - helidon-media-jsonp + org.glassfish.jersey.media + jersey-media-json-binding + runtime org.junit.jupiter diff --git a/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetResource.java b/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetResource.java index be6909a27b7..0714c58da94 100644 --- a/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetResource.java +++ b/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2022 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 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. @@ -16,15 +16,10 @@ package io.helidon.examples.integrations.micrometer.mp; -import java.util.Collections; - import io.micrometer.core.annotation.Counted; import io.micrometer.core.annotation.Timed; import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -63,8 +58,6 @@ public class GreetResource { static final String GETS_TIMER_NAME = "allGets"; private static final String GETS_TIMER_DESCRIPTION = "Tracks all GET operations"; - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -84,12 +77,12 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) @Timed(value = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, histogram = true) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -97,21 +90,21 @@ public JsonObject getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) @Counted(value = PERSONALIZED_GETS_COUNTER_NAME, description = PERSONALIZED_GETS_COUNTER_DESCRIPTION) @Timed(value = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, histogram = true) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message {@link GreetingMessage} containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -126,26 +119,18 @@ public JsonObject getMessage(@PathParam("name") String name) { @APIResponse(name = "normal", responseCode = "204", description = "Greeting updated"), @APIResponse(name = "missing 'greeting'", responseCode = "400", description = "JSON did not contain setting for 'greeting'")}) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } } diff --git a/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetingMessage.java b/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetingMessage.java new file mode 100644 index 00000000000..d5e11056131 --- /dev/null +++ b/examples/integrations/micrometer/mp/src/main/java/io/helidon/examples/integrations/micrometer/mp/GreetingMessage.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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. + */ + +package io.helidon.examples.integrations.micrometer.mp; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/integrations/micrometer/mp/src/test/java/io/helidon/examples/integrations/micrometer/mp/TestEndpoint.java b/examples/integrations/micrometer/mp/src/test/java/io/helidon/examples/integrations/micrometer/mp/TestEndpoint.java index 73b929f0593..180137d659c 100644 --- a/examples/integrations/micrometer/mp/src/test/java/io/helidon/examples/integrations/micrometer/mp/TestEndpoint.java +++ b/examples/integrations/micrometer/mp/src/test/java/io/helidon/examples/integrations/micrometer/mp/TestEndpoint.java @@ -20,7 +20,6 @@ import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import jakarta.inject.Inject; -import jakarta.json.JsonObject; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.MediaType; import org.junit.jupiter.api.Test; @@ -29,7 +28,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - @HelidonTest public class TestEndpoint { @@ -42,23 +40,23 @@ public class TestEndpoint { @Test public void pingGreet() { - JsonObject jsonObject = webTarget + GreetingMessage message = webTarget .path("/greet/Joe") .request(MediaType.APPLICATION_JSON_TYPE) - .get(JsonObject.class); + .get(GreetingMessage.class); - String responseString = jsonObject.getString("message"); + String responseString = message.getMessage(); assertThat("Response string", responseString, is("Hello Joe!")); Counter counter = registry.counter(PERSONALIZED_GETS_COUNTER_NAME); double before = counter.count(); - jsonObject = webTarget + message = webTarget .path("/greet/Jose") .request(MediaType.APPLICATION_JSON_TYPE) - .get(JsonObject.class); + .get(GreetingMessage.class); - responseString = jsonObject.getString("message"); + responseString = message.getMessage(); assertThat("Response string", responseString, is("Hello Jose!")); double after = counter.count(); diff --git a/examples/integrations/microstream/greetings-mp/pom.xml b/examples/integrations/microstream/greetings-mp/pom.xml index ba568a8e933..d8d8438ee70 100644 --- a/examples/integrations/microstream/greetings-mp/pom.xml +++ b/examples/integrations/microstream/greetings-mp/pom.xml @@ -39,6 +39,11 @@ io.helidon.integrations.microstream helidon-integrations-microstream-cdi + + org.glassfish.jersey.media + jersey-media-json-binding + runtime + diff --git a/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetResource.java b/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetResource.java index 9cee8ccc359..314f2df567a 100644 --- a/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetResource.java +++ b/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 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. @@ -16,13 +16,8 @@ package io.helidon.examples.integrations.microstream.greetings.mp; -import java.util.Collections; - import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -50,8 +45,6 @@ @RequestScoped public class GreetResource { - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - private final GreetingProvider greetingProvider; /** @@ -68,57 +61,50 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a default greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getGreeting(), who); - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } /** * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message JSON containing the new greeting * @return {@link Response} */ @Path("/greeting") @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.addGreeting(newGreeting); + greetingProvider.addGreeting(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } diff --git a/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetingMessage.java b/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetingMessage.java new file mode 100644 index 00000000000..e57afc8b6d6 --- /dev/null +++ b/examples/integrations/microstream/greetings-mp/src/main/java/io/helidon/examples/integrations/microstream/greetings/mp/GreetingMessage.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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. + */ +package io.helidon.examples.integrations.microstream.greetings.mp; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/integrations/microstream/greetings-mp/src/test/java/io/helidon/examples/integrations/microstream/greetings/mp/MicrostreamExampleGreetingsMpTest.java b/examples/integrations/microstream/greetings-mp/src/test/java/io/helidon/examples/integrations/microstream/greetings/mp/MicrostreamExampleGreetingsMpTest.java index 59945fc6240..622dfabcae6 100644 --- a/examples/integrations/microstream/greetings-mp/src/test/java/io/helidon/examples/integrations/microstream/greetings/mp/MicrostreamExampleGreetingsMpTest.java +++ b/examples/integrations/microstream/greetings-mp/src/test/java/io/helidon/examples/integrations/microstream/greetings/mp/MicrostreamExampleGreetingsMpTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 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. @@ -21,18 +21,14 @@ import io.helidon.microprofile.tests.junit5.HelidonTest; import jakarta.inject.Inject; -import jakarta.json.JsonObject; import jakarta.ws.rs.client.WebTarget; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertEquals; @HelidonTest -@Disabled("3.0.0-JAKARTA") // OpenAPI -// Caused by: java.lang.NoSuchMethodError: 'java.util.List org.jboss.jandex.ClassInfo.unsortedFields()' class MicrostreamExampleGreetingsMpTest { @Inject @@ -48,9 +44,9 @@ static void beforeAll() { @Test void testGreeting() { - JsonObject response = webTarget.path("/greet").request().get(JsonObject.class); + GreetingMessage response = webTarget.path("/greet").request().get(GreetingMessage.class); - assertEquals("Hello World!", response.getString("message"), "response should be 'Hello World' "); + assertEquals("Hello World!", response.getMessage(), "response should be 'Hello World' "); } } diff --git a/examples/metrics/filtering/mp/pom.xml b/examples/metrics/filtering/mp/pom.xml index 3040d960361..bea7ce28dff 100644 --- a/examples/metrics/filtering/mp/pom.xml +++ b/examples/metrics/filtering/mp/pom.xml @@ -41,7 +41,12 @@ io.helidon.media - helidon-media-jsonp + helidon-media-jsonb + + + org.glassfish.jersey.media + jersey-media-json-binding + runtime org.junit.jupiter diff --git a/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetResource.java b/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetResource.java index 72dfaf040b5..53422776139 100644 --- a/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetResource.java +++ b/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 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. @@ -16,13 +16,8 @@ package io.helidon.examples.metrics.filtering.mp; -import java.util.Collections; - import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -44,8 +39,6 @@ public class GreetResource { static final String TIMER_FOR_GETS = "timerForGets"; static final String COUNTER_FOR_PERSONALIZED_GREETINGS = "counterForPersonalizedGreetings"; - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -70,7 +63,7 @@ public GreetResource(GreetingProvider greetingConfig) { @GET @Produces(MediaType.APPLICATION_JSON) @Timed(name = TIMER_FOR_GETS, absolute = true) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -84,14 +77,14 @@ public JsonObject getDefaultMessage() { @GET @Produces(MediaType.APPLICATION_JSON) @Timed(name = TIMER_FOR_GETS, absolute = true) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message JSON containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -99,26 +92,19 @@ public JsonObject getMessage(@PathParam("name") String name) { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Counted(name = COUNTER_FOR_PERSONALIZED_GREETINGS, absolute = true) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } } diff --git a/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetingMessage.java b/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetingMessage.java new file mode 100644 index 00000000000..e0b889165e9 --- /dev/null +++ b/examples/metrics/filtering/mp/src/main/java/io/helidon/examples/metrics/filtering/mp/GreetingMessage.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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. + */ +package io.helidon.examples.metrics.filtering.mp; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/metrics/filtering/mp/src/test/java/io/helidon/examples/metrics/filtering/mp/MainTest.java b/examples/metrics/filtering/mp/src/test/java/io/helidon/examples/metrics/filtering/mp/MainTest.java index e439e784baf..b183bb405b5 100644 --- a/examples/metrics/filtering/mp/src/test/java/io/helidon/examples/metrics/filtering/mp/MainTest.java +++ b/examples/metrics/filtering/mp/src/test/java/io/helidon/examples/metrics/filtering/mp/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2023 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. @@ -23,15 +23,12 @@ import org.eclipse.microprofile.metrics.Counter; import org.eclipse.microprofile.metrics.MetricRegistry; import org.eclipse.microprofile.metrics.Timer; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @HelidonTest -@Disabled("3.0.0-JAKARTA") // OpenAPI -// Caused by: java.lang.NoSuchMethodError: 'java.util.List org.jboss.jandex.ClassInfo.unsortedFields()' public class MainTest { @Inject diff --git a/examples/microprofile/cors/pom.xml b/examples/microprofile/cors/pom.xml index 9a47d632ec9..c46a4d041aa 100644 --- a/examples/microprofile/cors/pom.xml +++ b/examples/microprofile/cors/pom.xml @@ -70,6 +70,11 @@ helidon-webclient test + + io.helidon.media + helidon-media-jsonb + test + diff --git a/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetResource.java b/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetResource.java index 269a0bc3c09..7a8b4bb2dae 100644 --- a/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetResource.java +++ b/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2022 Oracle and/or its affiliates. + * Copyright (c) 2020, 2023 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. @@ -16,15 +16,10 @@ package io.helidon.microprofile.examples.cors; -import java.util.Collections; - import io.helidon.microprofile.cors.CrossOrigin; import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.HttpMethod; @@ -49,8 +44,6 @@ @RequestScoped public class GreetResource { - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -70,11 +63,11 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -82,19 +75,19 @@ public JsonObject getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message {@link GreetingMessage} containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -109,18 +102,14 @@ public JsonObject getMessage(@PathParam("name") String name) { @APIResponse(name = "normal", responseCode = "204", description = "Greeting updated"), @APIResponse(name = "missing 'greeting'", responseCode = "400", description = "JSON did not contain setting for 'greeting'")}) - public Response updateGreeting(JsonObject jsonObject) { + public Response updateGreeting(GreetingMessage message) { - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } @@ -155,11 +144,8 @@ public void optionsForRetrievingUnnamedGreeting() { public void optionsForRetrievingNamedGreeting() { } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } } diff --git a/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetingMessage.java b/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetingMessage.java new file mode 100644 index 00000000000..82934aee6ae --- /dev/null +++ b/examples/microprofile/cors/src/main/java/io/helidon/microprofile/examples/cors/GreetingMessage.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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. + */ + +package io.helidon.microprofile.examples.cors; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/microprofile/cors/src/test/java/io/helidon/microprofile/examples/cors/TestCORS.java b/examples/microprofile/cors/src/test/java/io/helidon/microprofile/examples/cors/TestCORS.java index 22c1d72fdeb..7718bd34871 100644 --- a/examples/microprofile/cors/src/test/java/io/helidon/microprofile/examples/cors/TestCORS.java +++ b/examples/microprofile/cors/src/test/java/io/helidon/microprofile/examples/cors/TestCORS.java @@ -15,27 +15,21 @@ */ package io.helidon.microprofile.examples.cors; -import java.util.Collections; import java.util.List; import java.util.Optional; import io.helidon.common.http.Headers; import io.helidon.common.http.MediaType; import io.helidon.config.Config; -import io.helidon.media.jsonp.JsonpSupport; +import io.helidon.media.jsonb.JsonbSupport; import io.helidon.microprofile.server.Server; import io.helidon.webclient.WebClient; import io.helidon.webclient.WebClientRequestBuilder; import io.helidon.webclient.WebClientResponse; import io.helidon.webserver.cors.CrossOriginConfig; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; -import jakarta.json.JsonObjectBuilder; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -49,15 +43,8 @@ import static org.hamcrest.Matchers.not; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) -@Disabled("3.0.0-JAKARTA") // OpenAPI: Caused by: java.lang.NoSuchMethodError: -// 'java.util.List org.jboss.jandex.ClassInfo.unsortedFields()' public class TestCORS { - - private static final String JSON_MESSAGE_RESPONSE_LABEL = "message"; - private static final String JSON_NEW_GREETING_LABEL = "greeting"; - - private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Collections.emptyMap()); - private static final JsonpSupport JSONP_SUPPORT = JsonpSupport.create(); + private static final JsonbSupport JSONB_SUPPORT = JsonbSupport.create(); private static WebClient client; private static Server server; @@ -73,7 +60,7 @@ static void init() { .start(); client = WebClient.builder() .baseUri("http://localhost:" + server.port()) - .addMediaSupport(JSONP_SUPPORT) + .addMediaSupport(JSONB_SUPPORT) .build(); } @@ -220,17 +207,15 @@ private static WebClientResponse getResponse(String path, WebClientRequestBuilde } private static String fromPayload(WebClientResponse response) { - JsonObject json = response + GreetingMessage message = response .content() - .as(JsonObject.class) + .as(GreetingMessage.class) .await(); - return json.getString(JSON_MESSAGE_RESPONSE_LABEL); + return message.getMessage(); } - private static JsonObject toPayload(String message) { - JsonObjectBuilder builder = JSON_BF.createObjectBuilder(); - return builder.add(JSON_NEW_GREETING_LABEL, message) - .build(); + private static GreetingMessage toPayload(String message) { + return new GreetingMessage(message); } private static WebClientResponse putResponse(String path, String message) { return putResponse(path, message, client.put()); diff --git a/examples/microprofile/http-status-count-mp/pom.xml b/examples/microprofile/http-status-count-mp/pom.xml index ed002a2fa81..2471e3f1d56 100644 --- a/examples/microprofile/http-status-count-mp/pom.xml +++ b/examples/microprofile/http-status-count-mp/pom.xml @@ -38,14 +38,6 @@ io.helidon.microprofile.bundles helidon-microprofile - - com.fasterxml.jackson.core - jackson-databind - - - io.helidon.media - helidon-media-jackson - org.eclipse.microprofile.metrics microprofile-metrics-api diff --git a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetResource.java b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetResource.java index 787877fdfaa..5bcda14742f 100644 --- a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetResource.java +++ b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 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. @@ -69,11 +69,11 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link Message} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) - public Message getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -81,12 +81,12 @@ public Message getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link Message} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) - public Message getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } @@ -108,21 +108,20 @@ public Message getMessage(@PathParam("name") String name) { @APIResponse(name = "normal", responseCode = "204", description = "Greeting updated"), @APIResponse(name = "missing 'greeting'", responseCode = "400", description = "JSON did not contain setting for 'greeting'")}) - public Response updateGreeting(Message message) { + public Response updateGreeting(GreetingMessage message) { - if (message.getGreeting() == null || message.getGreeting().isEmpty()) { - Message error = new Message(); - error.setMessage("No greeting provided"); + if (message.getMessage() == null) { + GreetingMessage error = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(error).build(); } - greetingProvider.setMessage(message.getGreeting()); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private Message createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - return new Message(msg); + return new GreetingMessage(msg); } } diff --git a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/Message.java b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetingMessage.java similarity index 56% rename from examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/Message.java rename to examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetingMessage.java index e2500b697d5..d30fc5dc71d 100644 --- a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/Message.java +++ b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/GreetingMessage.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 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. @@ -18,58 +18,39 @@ /** * Greeting message. */ -public class Message { - +public class GreetingMessage { private String message; - private String greeting; - - /** - * Creates a new instance. - */ - public Message() { - } - /** - * Creates a new instance with a preset message. - * - * @param message initial message + * Create a new GreetingMessage instance. */ - public Message(String message) { - this.message = message; + public GreetingMessage() { } /** - * Sets the message content. + * Create a new GreetingMessage instance. * - * @param message the new message + * @param message message */ - public void setMessage(String message) { + public GreetingMessage(String message) { this.message = message; } /** + * Gets the message value. * - * @return the current message content + * @return message value */ public String getMessage() { - return this.message; - } - - /** - * Sets the greeting. - * - * @param greeting new greeting - */ - public void setGreeting(String greeting) { - this.greeting = greeting; + return message; } /** + * Sets the message value. * - * @return the greeting + * @param message message value to set */ - public String getGreeting() { - return this.greeting; + public void setMessage(String message) { + this.message = message; } } diff --git a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/SimpleGreetResource.java b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/SimpleGreetResource.java index f378a070362..7516812b1ed 100644 --- a/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/SimpleGreetResource.java +++ b/examples/microprofile/http-status-count-mp/src/main/java/io/helidon/examples/mp/httpstatuscount/SimpleGreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 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. @@ -55,13 +55,13 @@ public SimpleGreetResource(@ConfigProperty(name = "app.greeting") String message /** * Return a worldly greeting message. * - * @return {@link Message} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) - public Message getDefaultMessage() { + public GreetingMessage getDefaultMessage() { String msg = String.format("%s %s!", message, "World"); - Message message = new Message(); + GreetingMessage message = new GreetingMessage(); message.setMessage(msg); return message; } diff --git a/examples/microprofile/http-status-count-mp/src/test/java/io/helidon/examples/mp/httpstatuscount/MainTest.java b/examples/microprofile/http-status-count-mp/src/test/java/io/helidon/examples/mp/httpstatuscount/MainTest.java index 15190b89406..efe0fc78144 100644 --- a/examples/microprofile/http-status-count-mp/src/test/java/io/helidon/examples/mp/httpstatuscount/MainTest.java +++ b/examples/microprofile/http-status-count-mp/src/test/java/io/helidon/examples/mp/httpstatuscount/MainTest.java @@ -82,32 +82,32 @@ public void testHealth() throws Exception { @Test public void testGreet() throws Exception { - Message message = target + GreetingMessage message = target .path("simple-greet") .request() - .get(Message.class); + .get(GreetingMessage.class); assertThat(message.getMessage(), is("Hello World!")); } @Test public void testGreetings() throws Exception { - Message jsonMessage = target + GreetingMessage jsonMessage = target .path("greet/Joe") .request() - .get(Message.class); + .get(GreetingMessage.class); assertThat(jsonMessage.getMessage(), is("Hello Joe!")); try (Response r = target .path("greet/greeting") .request() - .put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { + .put(Entity.entity("{\"message\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { assertThat(r.getStatus(), is(204)); } jsonMessage = target .path("greet/Jose") .request() - .get(Message.class); + .get(GreetingMessage.class); assertThat(jsonMessage.getMessage(), is("Hola Jose!")); } diff --git a/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetResource.java b/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetResource.java index fb0867dc4a5..ad2259d5142 100644 --- a/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetResource.java +++ b/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetResource.java @@ -16,13 +16,8 @@ package io.helidon.microprofile.examples.openapi.basic; -import java.util.Collections; - import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -54,7 +49,8 @@ * curl -X GET http://localhost:8080/openapi * * Note that the output will include not only the annotated endpoints from this - * class but also an endpoint added by the {@link SimpleAPIModelReader}. + * class but also an endpoint added by the + * {@link io.helidon.microprofile.examples.openapi.basic.internal.SimpleAPIModelReader}. * * The message is returned as a JSON object. */ @@ -62,8 +58,6 @@ @RequestScoped public class GreetResource { - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -83,7 +77,7 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Operation(summary = "Returns a generic greeting", @@ -92,7 +86,7 @@ public GreetResource(GreetingProvider greetingConfig) { content = @Content(mediaType = "application/json", schema = @Schema(implementation = GreetingMessage.class))) @Produces(MediaType.APPLICATION_JSON) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -100,7 +94,7 @@ public JsonObject getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @@ -109,14 +103,14 @@ public JsonObject getDefaultMessage() { content = @Content(mediaType = "application/json", schema = @Schema(implementation = GreetingMessage.class))) @Produces(MediaType.APPLICATION_JSON) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message JSON containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -128,86 +122,26 @@ public JsonObject getMessage(@PathParam("name") String name) { description = "Conveys the new greeting prefix to use in building greetings", content = @Content( mediaType = "application/json", - schema = @Schema(implementation = GreetingUpdateMessage.class), + schema = @Schema(implementation = GreetingMessage.class), examples = @ExampleObject( name = "greeting", summary = "Example greeting message to update", value = "{\"greeting\": \"New greeting message\"}"))) @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } - - /** - * POJO defining the greeting message content exchanged with clients. - */ - public static class GreetingMessage { - - private String message; - - /** - * Gets the message value. - * - * @return message value - */ - public String getMessage() { - return message; - } - - /** - * Sets the message value. - * - * @param message message value to set - */ - public void setMessage(String message) { - this.message = message; - } - } - - /** - * POJO defining the greeting to use in future messages. - */ - public static class GreetingUpdateMessage { - - private String greeting; - - /** - * Gets the greeting value. - * - * @return greeting value - */ - public String getGreeting() { - return greeting; - } - - /** - * Sets the greeting value. - * - * @param greeting greeting value to set - */ - public void setGreeting(String greeting) { - this.greeting = greeting; - } - } - } diff --git a/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetingMessage.java b/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetingMessage.java new file mode 100644 index 00000000000..76c361b1e07 --- /dev/null +++ b/examples/microprofile/openapi-basic/src/main/java/io/helidon/microprofile/examples/openapi/basic/GreetingMessage.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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. + */ +package io.helidon.microprofile.examples.openapi.basic; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/microprofile/openapi-basic/src/test/java/io/helidon/microprofile/examples/openapi/basic/MainTest.java b/examples/microprofile/openapi-basic/src/test/java/io/helidon/microprofile/examples/openapi/basic/MainTest.java index d20e6ca7c4c..d17a7c8d130 100644 --- a/examples/microprofile/openapi-basic/src/test/java/io/helidon/microprofile/examples/openapi/basic/MainTest.java +++ b/examples/microprofile/openapi-basic/src/test/java/io/helidon/microprofile/examples/openapi/basic/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2022 Oracle and/or its affiliates. + * Copyright (c) 2019, 2023 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. @@ -53,32 +53,32 @@ void testHelloWorld() { Client client = ClientBuilder.newClient(); - JsonObject jsonObject = client + GreetingMessage message = client .target(getConnectionString("/greet")) .request() - .get(JsonObject.class); - assertThat("default message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("default message", message.getMessage(), is("Hello World!")); - jsonObject = client + message = client .target(getConnectionString("/greet/Joe")) .request() - .get(JsonObject.class); - assertThat("hello Joe message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hello Joe message", message.getMessage(), is("Hello Joe!")); try (Response r = client .target(getConnectionString("/greet/greeting")) .request() - .put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { + .put(Entity.entity("{\"message\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { assertThat("PUT status code", r.getStatus(), is(204)); } - jsonObject = client + message = client .target(getConnectionString("/greet/Jose")) .request() - .get(JsonObject.class); - assertThat("hola Jose message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hola Jose message", message.getMessage(), is("Hola Jose!")); client.close(); diff --git a/examples/quickstarts/helidon-quickstart-mp/pom.xml b/examples/quickstarts/helidon-quickstart-mp/pom.xml index 781af6eb03c..e6182cfceeb 100644 --- a/examples/quickstarts/helidon-quickstart-mp/pom.xml +++ b/examples/quickstarts/helidon-quickstart-mp/pom.xml @@ -36,6 +36,11 @@ io.helidon.microprofile.bundles helidon-microprofile + + org.glassfish.jersey.media + jersey-media-json-binding + runtime + org.jboss jandex diff --git a/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java index 1eee0293d3f..50e8335a26c 100644 --- a/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java +++ b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java @@ -16,12 +16,7 @@ package io.helidon.examples.quickstart.mp; -import java.util.Collections; - import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -55,8 +50,6 @@ @Path("/greet") public class GreetResource { - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -76,7 +69,7 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Operation(summary = "Returns a generic greeting", @@ -85,7 +78,7 @@ public GreetResource(GreetingProvider greetingConfig) { content = @Content(mediaType = "application/json", schema = @Schema(implementation = GreetingMessage.class))) @Produces(MediaType.APPLICATION_JSON) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -93,7 +86,7 @@ public JsonObject getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @@ -102,14 +95,14 @@ public JsonObject getDefaultMessage() { content = @Content(mediaType = "application/json", schema = @Schema(implementation = GreetingMessage.class))) @Produces(MediaType.APPLICATION_JSON) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message JSON containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -124,52 +117,19 @@ public JsonObject getMessage(@PathParam("name") String name) { @APIResponse(name = "normal", responseCode = "204", description = "Greeting updated"), @APIResponse(name = "missing 'greeting'", responseCode = "400", description = "JSON did not contain setting for 'greeting'")}) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - return JSON.createObjectBuilder() - .add("message", msg) - .build(); - } - - /** - * POJO defining the greeting message content. - */ - public static class GreetingMessage { - - private String message; - - /** - * Gets the message value. - * - * @return message value - */ - public String getMessage() { - return message; - } - - /** - * Sets the message value. - * - * @param message message value to set - */ - public void setMessage(String message) { - this.message = message; - } + return new GreetingMessage(msg); } } diff --git a/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java new file mode 100644 index 00000000000..eab1bac7963 --- /dev/null +++ b/examples/quickstarts/helidon-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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. + */ +package io.helidon.examples.quickstart.mp; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/quickstarts/helidon-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java b/examples/quickstarts/helidon-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java index 5ee3aa899f1..19d6292e621 100644 --- a/examples/quickstarts/helidon-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java +++ b/examples/quickstarts/helidon-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022 Oracle and/or its affiliates. + * Copyright (c) 2018, 2023 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. @@ -19,7 +19,6 @@ import io.helidon.microprofile.tests.junit5.HelidonTest; import jakarta.inject.Inject; -import jakarta.json.JsonObject; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.MediaType; @@ -41,28 +40,28 @@ class MainTest { @Test void testHelloWorld() { - JsonObject jsonObject = target.path("/greet") + GreetingMessage message = target.path("/greet") .request() - .get(JsonObject.class); - assertThat("default message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("default message", message.getMessage(), is("Hello World!")); - jsonObject = target.path("/greet/Joe") + message = target.path("/greet/Joe") .request() - .get(JsonObject.class); - assertThat("hello Joe message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hello Joe message", message.getMessage(), is("Hello Joe!")); try (Response r = target.path("/greet/greeting") .request() - .put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { + .put(Entity.entity("{\"message\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { assertThat("PUT status code", r.getStatus(), is(204)); } - jsonObject = target.path("/greet/Jose") + message = target.path("/greet/Jose") .request() - .get(JsonObject.class); - assertThat("hola Jose message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hola Jose message", message.getMessage(), is("Hola Jose!")); try (Response r = target.path("/metrics") diff --git a/examples/quickstarts/helidon-standalone-quickstart-mp/pom.xml b/examples/quickstarts/helidon-standalone-quickstart-mp/pom.xml index 7d60b13aadb..fc3db5b688f 100644 --- a/examples/quickstarts/helidon-standalone-quickstart-mp/pom.xml +++ b/examples/quickstarts/helidon-standalone-quickstart-mp/pom.xml @@ -68,6 +68,11 @@ io.helidon.microprofile.bundles helidon-microprofile + + org.glassfish.jersey.media + jersey-media-json-binding + runtime + org.jboss jandex diff --git a/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java b/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java index ae033aa308f..5de0bde8832 100644 --- a/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java +++ b/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022 Oracle and/or its affiliates. + * Copyright (c) 2018, 2023 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. @@ -16,13 +16,8 @@ package io.helidon.examples.quickstart.mp; -import java.util.Collections; - import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; -import jakarta.json.Json; -import jakarta.json.JsonBuilderFactory; -import jakarta.json.JsonObject; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.GET; import jakarta.ws.rs.PUT; @@ -56,8 +51,6 @@ @RequestScoped public class GreetResource { - private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); - /** * The greeting message provider. */ @@ -77,11 +70,11 @@ public GreetResource(GreetingProvider greetingConfig) { /** * Return a worldly greeting message. * - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getDefaultMessage() { + public GreetingMessage getDefaultMessage() { return createResponse("World"); } @@ -89,19 +82,19 @@ public JsonObject getDefaultMessage() { * Return a greeting message using the name that was provided. * * @param name the name to greet - * @return {@link JsonObject} + * @return {@link GreetingMessage} */ @Path("/{name}") @GET @Produces(MediaType.APPLICATION_JSON) - public JsonObject getMessage(@PathParam("name") String name) { + public GreetingMessage getMessage(@PathParam("name") String name) { return createResponse(name); } /** * Set the greeting to use in future messages. * - * @param jsonObject JSON containing the new greeting + * @param message JSON containing the new greeting * @return {@link Response} */ @Path("/greeting") @@ -116,26 +109,19 @@ public JsonObject getMessage(@PathParam("name") String name) { @APIResponse(name = "normal", responseCode = "204", description = "Greeting updated"), @APIResponse(name = "missing 'greeting'", responseCode = "400", description = "JSON did not contain setting for 'greeting'")}) - public Response updateGreeting(JsonObject jsonObject) { - - if (!jsonObject.containsKey("greeting")) { - JsonObject entity = JSON.createObjectBuilder() - .add("error", "No greeting provided") - .build(); + public Response updateGreeting(GreetingMessage message) { + if (message.getMessage() == null) { + GreetingMessage entity = new GreetingMessage("No greeting provided"); return Response.status(Response.Status.BAD_REQUEST).entity(entity).build(); } - String newGreeting = jsonObject.getString("greeting"); - - greetingProvider.setMessage(newGreeting); + greetingProvider.setMessage(message.getMessage()); return Response.status(Response.Status.NO_CONTENT).build(); } - private JsonObject createResponse(String who) { + private GreetingMessage createResponse(String who) { String msg = String.format("%s %s!", greetingProvider.getMessage(), who); - return JSON.createObjectBuilder() - .add("message", msg) - .build(); + return new GreetingMessage(msg); } } diff --git a/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java b/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java new file mode 100644 index 00000000000..eab1bac7963 --- /dev/null +++ b/examples/quickstarts/helidon-standalone-quickstart-mp/src/main/java/io/helidon/examples/quickstart/mp/GreetingMessage.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 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. + */ +package io.helidon.examples.quickstart.mp; + +/** + * POJO defining the greeting message content. + */ +@SuppressWarnings("unused") +public class GreetingMessage { + private String message; + + /** + * Create a new GreetingMessage instance. + */ + public GreetingMessage() { + } + + /** + * Create a new GreetingMessage instance. + * + * @param message message + */ + public GreetingMessage(String message) { + this.message = message; + } + + /** + * Gets the message value. + * + * @return message value + */ + public String getMessage() { + return message; + } + + /** + * Sets the message value. + * + * @param message message value to set + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/examples/quickstarts/helidon-standalone-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java b/examples/quickstarts/helidon-standalone-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java index 5ee3aa899f1..19d6292e621 100644 --- a/examples/quickstarts/helidon-standalone-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java +++ b/examples/quickstarts/helidon-standalone-quickstart-mp/src/test/java/io/helidon/examples/quickstart/mp/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022 Oracle and/or its affiliates. + * Copyright (c) 2018, 2023 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. @@ -19,7 +19,6 @@ import io.helidon.microprofile.tests.junit5.HelidonTest; import jakarta.inject.Inject; -import jakarta.json.JsonObject; import jakarta.ws.rs.client.Entity; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.MediaType; @@ -41,28 +40,28 @@ class MainTest { @Test void testHelloWorld() { - JsonObject jsonObject = target.path("/greet") + GreetingMessage message = target.path("/greet") .request() - .get(JsonObject.class); - assertThat("default message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("default message", message.getMessage(), is("Hello World!")); - jsonObject = target.path("/greet/Joe") + message = target.path("/greet/Joe") .request() - .get(JsonObject.class); - assertThat("hello Joe message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hello Joe message", message.getMessage(), is("Hello Joe!")); try (Response r = target.path("/greet/greeting") .request() - .put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { + .put(Entity.entity("{\"message\" : \"Hola\"}", MediaType.APPLICATION_JSON))) { assertThat("PUT status code", r.getStatus(), is(204)); } - jsonObject = target.path("/greet/Jose") + message = target.path("/greet/Jose") .request() - .get(JsonObject.class); - assertThat("hola Jose message", jsonObject.getString("message"), + .get(GreetingMessage.class); + assertThat("hola Jose message", message.getMessage(), is("Hola Jose!")); try (Response r = target.path("/metrics")