Learn QUARKUS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Quarkus Simple Counter API
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/counter")
public class CounterResource {
private int count = 0
@GET
@Produces(MediaType.APPLICATION_JSON)
public int getCount() {
return count
}
@POST
@Path("/increment")
@Produces(MediaType.APPLICATION_JSON)
public int increment() {
return ++count
}
@POST
@Path("/decrement")
@Produces(MediaType.APPLICATION_JSON)
public int decrement() {
return --count
}
@POST
@Path("/reset")
@Produces(MediaType.APPLICATION_JSON)
public int reset() {
count = 0
return count
}
}
Demonstrates a simple Quarkus REST API with a counter using JAX-RS annotations and in-memory state.
2
Quarkus Hello World API
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello, Quarkus!"
}
}
A minimal Quarkus REST API returning Hello World.
3
Quarkus Query Parameter Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/greet")
public class GreetResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String greet(@QueryParam("name") String name) {
return "Hello, " + (name != null ? name : "Guest")
}
}
Demonstrates query parameters in Quarkus REST API.
4
Quarkus JSON Response Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
import java.util.Map
@ApplicationScoped
@Path("/json")
public class JsonResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> json() {
return Map.of("message", "Hello, Quarkus JSON")
}
}
Return JSON response from Quarkus REST API.
5
Quarkus POST Form Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/form")
public class FormResource {
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String handleForm(@FormParam("name") String name) {
return "Hello, " + name
}
}
Handle POST requests with form parameters.
6
Quarkus Path Parameter Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/user")
public class UserResource {
@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getUser(@PathParam("id") String id) {
return "User ID: " + id
}
}
Use path parameters in Quarkus REST API.
7
Quarkus Error Handling Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/error")
public class ErrorResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String error() {
throw new WebApplicationException("This is an error", 400)
}
}
Demonstrates basic exception handling in Quarkus.
8
Quarkus Redirect Example
import javax.ws.rs.*
import javax.ws.rs.core.Response
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("/redirect")
public class RedirectResource {
@GET
public Response redirect() {
return Response.seeOther(URI.create("/counter")).build()
}
}
Redirect request to another endpoint.
9
Quarkus Multiple Routes Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
@ApplicationScoped
@Path("")
public class MultiResource {
@GET
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
public String home() {
return "Home"
}
@GET
@Path("/about")
@Produces(MediaType.TEXT_PLAIN)
public String about() {
return "About"
}
}
Handle multiple routes in one Quarkus application.
10
Quarkus Injected Bean Example
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.enterprise.context.ApplicationScoped
import javax.inject.Inject
@ApplicationScoped
@Path("/bean")
public class BeanResource {
@Inject
CounterService service
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getCount() {
return "Count: " + service.getCount()
}
}
@ApplicationScoped
class CounterService {
private int count = 0
public int getCount() { return count }
public int increment() { return ++count }
}
Use CDI to inject a bean into a Quarkus REST API.