use of jakarta.json.JsonObject in project helidon by oracle.
the class GreetService method processErrors.
private static <T> T processErrors(Throwable ex, ServerRequest request, ServerResponse response) {
if (ex.getCause() instanceof JsonException) {
LOGGER.log(Level.FINE, "Invalid JSON", ex);
JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "Invalid JSON").build();
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
} else {
LOGGER.log(Level.FINE, "Internal error", ex);
JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "Internal error").build();
response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(jsonErrorObject);
}
return null;
}
use of jakarta.json.JsonObject in project helidon by oracle.
the class GreetService method updateGreetingFromJson.
private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
if (!jo.containsKey("greeting")) {
JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "No greeting provided").build();
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
return;
}
greeting.set(jo.getString("greeting"));
response.status(Http.Status.NO_CONTENT_204).send();
}
use of jakarta.json.JsonObject in project helidon by oracle.
the class FileServiceTest method testList.
@Test
@Order(3)
public void testList() {
WebClientResponse response = webClient.get().contentType(MediaType.APPLICATION_JSON).request().await();
assertThat(response.status().code(), Matchers.is(200));
JsonObject json = response.content().as(JsonObject.class).await();
assertThat(json, Matchers.is(notNullValue()));
List<String> files = json.getJsonArray("files").getValuesAs(v -> ((JsonString) v).getString());
assertThat(files, hasItem("foo.txt"));
}
use of jakarta.json.JsonObject in project helidon by oracle.
the class ImplicitHelloWorldTest method testJsonResource.
@Test
void testJsonResource() {
JsonObject jsonObject = target.path("/helloworld/unit").request().get(JsonObject.class);
assertAll("JSON fields must match expected injection values", () -> assertThat("Name from request", jsonObject.getString("name"), is("unit")), () -> assertThat("Request id from CDI provider", jsonObject.getInt("requestId"), is(1)), () -> assertThat("App name from config", jsonObject.getString("appName"), is("Hello World Application")), () -> assertThat("Logger name", jsonObject.getString("logger"), is(HelloWorldResource.class.getName())));
}
use of jakarta.json.JsonObject in project helidon by oracle.
the class GreetService method updateGreetingFromJson.
private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
if (!jo.containsKey("greeting")) {
JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "No greeting provided").build();
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
return;
}
greeting.set(jo.getString("greeting"));
response.status(Http.Status.NO_CONTENT_204).send();
}
Aggregations