Chapter 3. Developing RESTful Services

Quarkus integrates with RESTEasy, a JAX-RS implementation to define REST APIs. In this chapter, you’ll learn how to develop RESTful web services in Quarkus. We’ll cover the following topics:

  • How to use JAX-RS for creating CRUD services

  • How to enable CORS for requesting resources from other domains

  • How to implement reactive routes

  • How to implement filters to manipulate requests and responses

3.1 Creating a Simple REST API Endpoint

Problem

You want to create a REST API endpoint with CRUD operations.

Solution

Use the JAX-RS GreetingResource resource generated previously and fill it with JAX-RS annotations.

JAX-RS is the default framework used in Quarkus to define REST endpoints. All of the JAX-RS annotations are already correctly on your classpath. You will want to use the HTTP verb annotations (@GET, @POST, @PUT, @DELETE) to declare the HTTP verb(s) that the endpoint methods will listen to. Of course, you will need the @Path annotation to define the URI relative to the rest of the application for your endpoint.

Open org.acme.quickstart.GreetingResource.java:

package org.acme.quickstart;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello") 1
public class GreetingResource {
    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String hello() {
        return "hello" ...

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.