Build a JSON POST contract in Quarkus REST: Jackson and response checks
A Quarkus REST endpoint needs a JSON extension to read and write Java objects as JSON. Choose quarkus-rest-jackson for Jackson or quarkus-rest-jsonb for JSON-B. This example uses Jackson for POST /messages/echo, declares both media types, and checks status and fields with a RestAssured test. The current official guide requires JDK 17+ and Maven 3.9.16; docs were reviewed on 2026-11-20.
A Quarkus REST endpoint needs a JSON extension to read and write Java objects as JSON. Choose quarkus-rest-jackson for Jackson or quarkus-rest-jsonb for JSON-B. This example uses Jackson for POST /messages/echo, declares both media types, and checks status and fields with a RestAssured test. The current official guide requires JDK 17+ and Maven 3.9.16; docs were reviewed on 2026-11-20.
Choose an extension and create the project
Jackson fits projects already using that library; JSON-B is the Jakarta JSON Binding alternative. Choose one provider to match the project instead of adding both without a reason.
Create a project with the Quarkus CLI and Jackson support:
quarkus create app org.acme:rest-json-quickstart --extension=rest-jackson --no-code
Set-Location rest-json-quickstartFor JSON-B, use --extension=rest-jsonb. The generated Maven project includes the JSON extension. If its template does not include test setup, add quarkus-junit and rest-assured as test dependencies. Follow the versions generated by the current project tooling.
Add a JSON model and endpoint
Create src/main/java/org/acme/rest/json/Message.java:
package org.acme.rest.json;
public class Message {
public String name;
public String text;
public Message() {
}
public Message(String name, String text) {
this.name = name;
this.text = text;
}
}This simple bean includes a no-argument constructor. Then create src/main/java/org/acme/rest/json/MessageResource.java:
package org.acme.rest.json;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/messages")
public class MessageResource {
@POST
@Path("/echo")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Message echo(Message message) {
return message;
}
}@Consumes states that the request body is JSON; @Produces states the response is JSON. With a JSON extension, Quarkus defaults most object return values to application/json, but explicit annotations make this endpoint's contract clear. Some types such as String have different defaults.
Verify the HTTP contract
Add src/test/java/org/acme/rest/json/MessageResourceTest.java:
package org.acme.rest.json;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
@QuarkusTest
class MessageResourceTest {
@Test
void echoesJsonMessage() {
given()
.contentType(ContentType.JSON)
.accept(ContentType.JSON)
.body("{\"name\":\"Mina\",\"text\":\"hello\"}")
.when()
.post("/messages/echo")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("name", is("Mina"))
.body("text", is("hello"));
}
}@QuarkusTest starts the application before JUnit runs. The assertions check status, response media type, and fields rather than JSON key order. By default, Quarkus runs tests on port 8081 to avoid conflicting with the development server.
Run and troubleshoot
In PowerShell, run:
.\mvnw.cmd testOn failure, use Maven output to distinguish compilation, startup, status, and JSON-path errors. For 415, compare request Content-Type with @Consumes; for 406, compare Accept with @Produces. If JSON conversion fails, check that the chosen extension is in the build file and that the model supports the selected serializer.
To start dev mode and send a direct request, run .\mvnw.cmd quarkus:dev in one terminal, then use another:
curl.exe -i -X POST http://localhost:8080/messages/echo -H "Content-Type: application/json" -H "Accept: application/json" -d '{"name":"Mina","text":"hello"}'Expect status 200 and a JSON object containing name and text. If shell quoting damages the body, use Invoke-RestMethod with a hashtable converted by ConvertTo-Json. The development server uses port 8080, while tests default to 8081.
Jackson unknown fields
Quarkus's default Jackson configuration ignores unknown JSON properties. If the API should reject additional fields, consider this setting in application.properties:
quarkus.jackson.fail-on-unknown-properties=trueStrict input can reduce compatibility with clients that send new fields, so define the allowed payload and version policy deliberately. This setting is Jackson-specific; do not apply it to a JSON-B project.
Official sources
[Writing JSON REST services](https://quarkus.io/guides/rest-json/)
[Creating your first application](https://quarkus.io/guides/getting-started/)
[Testing your application](https://quarkus.io/guides/getting-started-testing/)