Chapter 5. Programming Model
In this chapter, you’ll learn about topics related to the programming model of Quarkus. Quarkus is unique in the programming model it follows. Unlike some other frameworks, Quarkus allows you to mix and match both an imperative model, using CDI, and a reactive model, using SmallRye Mutiny. Chapter 15 is dedicated to using the reactive approach. At times, you may find yourself needing both approaches, so it is good to know how to utilize each.
In this chapter, we’re focusing on the imperative model, so you’ll learn the following:
-
How to marshal/unmarshal JSON and XML documents
-
How to validate request parameters
-
How to use the CDI spec as a context and dependency injection solution
-
How to write tests for a Quarkus service
5.1 Marshalling/Unmarshalling JSON
Problem
You want to marshall/unmarshall JSON documents to/from Java objects.
Solution
Use the JSON-B
specification or Jackson project to marshall and unmarshall JSON documents from/to Java objects.
When you are creating a REST API, you usually use JSON as a data format to exchange information. So far, you’ve seen examples of returning only simple plain-text responses; but in this recipe, you’ll learn how to start using JSON as the data format for the body of the request and as a response.
The first thing you need to do is register the JSON-B extension in the pom.xml. Open a terminal window, and from the root directory of the project run the following:
./mvnw quarkus:add-extension -Dextensions="quarkus-resteasy-jsonb" ...
Get Quarkus Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.