Skip to content

Commit

Permalink
3.x: Use JSON-B instead of JSON-P in MP quickstarts (#7487)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
tvallin authored Sep 5, 2023
1 parent 6e540ea commit e2bd18e
Show file tree
Hide file tree
Showing 32 changed files with 599 additions and 403 deletions.
5 changes: 3 additions & 2 deletions examples/integrations/micrometer/mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
<artifactId>microprofile-openapi-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.media</groupId>
<artifactId>helidon-media-jsonp</artifactId>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand All @@ -84,34 +77,34 @@ 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");
}

/**
* 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")
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +28,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;


@HelidonTest
public class TestEndpoint {

Expand All @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions examples/integrations/microstream/greetings-mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<groupId>io.helidon.integrations.microstream</groupId>
<artifactId>helidon-integrations-microstream-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -50,8 +45,6 @@
@RequestScoped
public class GreetResource {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());

private final GreetingProvider greetingProvider;

/**
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit e2bd18e

Please sign in to comment.