- ⌘⌥L to format
- ⇧⌘T to flip between tests and implementation
- fn-cmd-F1 to switch screen mirroring on and off
- Go to https://code.quarkus.io
- Just choose the top option, Rest
- Show that the template doesn't have much in it, except for maven, because can't make less maven less verbose. :)
- Copy the folder path from the Downloads folder
- In a terminal run
idea <path>
- MAKE THE FONT BIGGER
- Show the generated dockerfiles, because Quarkus is Kubernetes-native.
- Explain that actually we prefer jib because it's easier to keep it up to date, compared to a file which is generated once.
- Launch in dev mode
mvn quarkus:dev
-
Click ‘r’ to run tests
-
Visit http://localhost:8080, which will show a get static page
-
Navigate to the Dev UI
-
Show the configuration and continuous testing tabs
-
I didn't write this code, what even is my endpoint?
-
Find the endpoint by viewing the Rest Easy extension panel's endpoint scores
-
Visit http://localhost:8080/hello. It should work!
- Make a change. Try changing the greeting, using a
hello
live template. - Can we make it more exciting? Add a queryparam in
GreetingResource
(aquery
live template is useful for this, and also agreeting
live template for hello in other languages)
public String hello(@QueryParam("name") String name) {
return "Hello " + name;
}
- Visit http://localhost:8080/hello?name=yourname
- If you forget the space first time, no worries! It shows the reload when fixed.
- We got so excited by the UI, we did not notice a test is failing. Fix it using
.body(containsString("Hello"));
- To add the persistence libraries, run
quarkus ext add hibernate-orm-panache jdbc-postgresql
- Now do some persistence. Make a greeting:
@Entity
public class Greeting extends PanacheEntity {
String name;
}
- This is using Panache, a layer on top of hibernate.
PanacheEntity
allows us to use the active record pattern and eliminate a lot of boilerplate. - Then change the
Get
method to persist it. Construct the entity, add@Transactional
and persist.
We're aiming for the following:
@GET
@Transactional
@Produces(MediaType.TEXT_PLAIN)
public String hello(@QueryParam("name") String name) {
Greeting greeting = new Greeting();
greeting.name = name;
greeting.persist();
return "Hello " + name;
}
Normally you wouldn't use a GET
to persist something to the database, but we're being lazy. Let's pretend it's monitoring.
- Is the data going in? Add a second method. It needs to be in two steps to avoid a cast. Use a
names
live template.
@GET
@Path("names")
@Produces(MediaType.TEXT_PLAIN)
public String names() {
List<Greeting> greetings = Greeting.listAll();
String names = greetings.stream().map(g-> g.name)
.collect(Collectors.joining (", "));
return "I've seen " + names;
}
- This is being stored in a database, but we didn’t set one up.
- Run
podman ps
- Kill the container running the database.
- Quarkus is unhappy, but everything comes back if Quarkus is restarted.
- Run
podman ps
again to show it’s back
What if we want the database not to be empty on a restart?
-
Let’s make it look like we have more friends.
-
Make a
src/main/resources/import.sql
file. -
Populate it with the following:
INSERT INTO Greeting(id, name)
VALUES (nextval('Greeting_SEQ'), 'Alice');
INSERT INTO Greeting(id, name)
VALUES (nextval('Greeting_SEQ'), 'Bob');
(An IntelliJ live template called sql
is handy for this.)
- The data will update without a restart.
- We can also go to the dev UI, and navigate to hibernate, persistence units, and then click on it, to get a create script.
To build a native application, we can run
quarkus build --native -DskipTests
Then use tab complete for target/code-with-quarkus-1.0.0-SNAPSHOT-runner
However, this would fail at runtime, because no database is configured.