[JAVA] I tried to make a Web API that connects to DB with Quarkus


I will leave a summary of the contents that confirmed the usability of Quarkus, which is a hot topic in the streets. Only Get Started was not enough, so I tried to make PostgreSQL that was set up locally as a REST Web API that operates with Hibernate.

Advance preparation

Maven project

Complete QUARKUS --CREATING YOUR FIRST APPLICATION first. The state of the project looks like this.

$ tree ./
├── pom.xml
├── quarkus_quickstart.iml
└── src
    ├── main
    │   ├── docker
    │   │   └── Dockerfile
    │   ├── java
    │   │   └── com
    │   │       └── dsert
    │   │           └── quickstart
    │   │               ├── GreetingResource.java
    │   │               └── GreetingService.java
    │   └── resources
    │       ├── META-INF
    │       │   ├── microprofile-config.properties
    │       │   └── resources
    │       │       └── index.html
    │       └── application.properties
    └── test
        └── java
            └── com
                └── dsert
                    └── quickstart
                        ├── GreetingResourceTest.java
                        └── NativeGreetingResourceIT.java

PostgreSQL Make a table or something like that. The DDL generated by IntelliJ looks like the following.

create database quarkus	with owner quarkus;

create table if not exists users
	id serial not null
		constraint users_pk
			primary key,
	name varchar(255) not null

Implementation procedure


Add the Hibernate and PostgreSQL dependencies to pom.xml, referring to the QUARKUS --EXTENSIONS section. Also, since the request and response in JSON is not possible with the dependency as Get Started, change quarkus-resteasy to quarkus-resteasy-jsonb.



  <!-- Hibernate ORM specific dependencies -->

  <!-- JDBC driver dependencies -->

Environmental value

As described in QUARKUS --APPLICATION CONFIGURATION GUIDE, you can manage environment values in application.properties. Describe the information required for DB connection as follows.


quarkus.datasource.url: jdbc:postgresql://localhost:5432/quarkus
quarkus.datasource.driver: org.postgresql.Driver
quarkus.datasource.username: quarkus
quarkus.datasource.password: quarkus-pass

Source code

Entity Create an Entity object that fits the table to map the data.


package com.dsert.quickstart;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "users")
public class User {
    private int id;
    private String name;

    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

Service Create a service class that operates the DB.


package com.dsert.quickstart;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;

public class UserService {
    EntityManager em;

    public void createUser(String name) {
        User user = new User();

    public List<User> getUsers() {
        return this.em.createNativeQuery("select * from users", User.class).getResultList();

Resource Create a resource class that handles HTTP requests.


package com.dsert.quickstart;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class UserResource {
    UserService service;

    public Map<String, String> createUser(User user) {
        return Collections.singletonMap("message", "OK");

    public List<User> getUsers() {
        return this.service.getUsers();

Operation check

Application launch

I want to start it easily, so I start it in development mode instead of DockerImage.

mvn compile quarkus:dev                                                                                                                                                                    6.2m
[INFO] Scanning for projects...
[INFO] -------------------< com.d-sert:quarkus_quickstart >--------------------
[INFO] Building quarkus_quickstart 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ quarkus_quickstart ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ quarkus_quickstart ---
[INFO] Nothing to compile - all classes are up to date
[INFO] --- quarkus-maven-plugin:0.11.0:dev (default-cli) @ quarkus_quickstart ---
[INFO] Using servlet resources quarkus_quickstart/src/main/resources/META-INF/resources
Listening for transport dt_socket at address: 5005
2019-03-13 08:49:58,673 INFO  [io.qua.dep.QuarkusAugmentor](main) Beginning quarkus augmentation
2019-03-13 08:49:59,282 INFO  [io.qua.dep.QuarkusAugmentor](main) Quarkus augmentation completed in 609ms
2019-03-13 08:50:05,128 INFO  [io.quarkus](main) Quarkus 0.11.0 started in 6.638s. Listening on:
2019-03-13 08:50:05,130 INFO  [io.quarkus](main) Installed features: [agroal, cdi, hibernate-orm, jdbc-postgresql, narayana-jta, resteasy, resteasy-jsonb]

HTTP request


$ curl -X POST -H 'Content-Type: application/json' 'http://localhost:8080/users' -d '{"name": "sert"}'


$ curl 'http://localhost:8080/users'


I really wanted to connect to MySQL, but the MySQL driver is not in the Extension, I did it with PostgreSQL because I couldn't connect well with MariaDB Driver. I will try to revenge with MySQL soon.

Recommended Posts

I tried to make a Web API that connects to DB with Quarkus
I tried to make a group function (bulletin board) with Rails
I tried to make Basic authentication with Java
[iOS] I tried to make a processing application like Instagram with Swift
I tried to break a block with java (1)
[LINE @] I tried to make a Japanese calendar Western calendar conversion BOT [Messaging API]
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Android] I tried to make a material list screen with ListView + Bottom Sheet
I tried to clone a web application full of bugs with Spring Boot
I tried to make a login function in Java
I tried to make FizzBuzz that is uselessly flexible
I tried to draw animation with Blazor + canvas API
I tried to make a program that searches for the target class from the process that is overloaded with Java
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to link chat with Minecraft server with Discord API
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I tried to create a padrino development environment with Docker
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
I want to write a loop that references an index with Java 8's Stream API
I tried to interact with Java
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile
A story that I struggled to challenge a competition professional with Java
I want to make a button with a line break with link_to [Note]
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to make an Android application with MVC now (Java)
[Java] I tried to make a maze by the digging method ♪
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I want to develop a web application!
I tried playing with BottomNavigationView a little ①
I tried to summarize the Stream API
I tried to implement ModanShogi with Kinx
I tried to make a parent class of a value object in Ruby
I tried to make a simple face recognition Android application using OpenCV
I tried learning Java with a series that beginners can understand clearly
I tried to make an automatic backup with pleasanter + PostgreSQL + SSL + docker
Java beginner tried to make a simple web application using Spring Boot
I tried to build a Firebase application development environment with Docker in 2020
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I made a virtual currency arbitrage bot and tried to make money
I tried to make a talk application in Java using AI "A3RT"
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
I tried to make a machine learning application with Dash (+ Docker) part1 ~ Environment construction and operation check ~
A note that I gave up trying to make a custom annotation for Lombok
I tried to easily put CentOS-7 in a PC that I no longer need
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to solve 10 selected past questions that should be solved after registering with AtCoder with Java, Stream API
I tried to manage struts configuration with Coggle
I tried to implement a function equivalent to Felica Lite with HCE-F of Android
I tried to manage login information with JMX