This website was always meant to be a hobby. I have never expected a huge audience or felt obligated to make it a success. However, I still feel like I should have put more effort into keeping a record of my hobby projects. So let me tell you a few things about quarkus and how I use it.
The easiest way to start is by using https://code.quarkus.io/ , choosing what you need for your project, and downloading the result. In my case, I used mainly RESTEasy JAX-RS, RESTEasy Jackson, SmallRye OpenAPI, JDBC Driver MySQL, JDBC Driver H2. The configuration can vary based on your project.
I need RESTEasy for the REST endpoints and Jackson is for using the JSON format. REST endpoints are just pieces of code, that enable me to return data when I call a path. For example, this would return “Hello World!” after I open http://localhost:8080/hello in a browser:
@Path("/hello")
public class HelloResource {
@GET
public Response sayHello() {
return Response.ok("Hello World!").build();
}
}
I can run my server locally by typing ./mvnw compile quarkus:dev in the console. It should run on port 8080 by default.
JSON is just a format for storing and transporting data. Here is a JSON representing a project object on this website:
{
"id": 15,
"title": "Sudoku Solver and Generator",
"url": "sudoku-solver",
"createdDate": "24 March 2022",
"publishedDate": null,
"updatedDate": "24 March 2022",
"username": "furcino",
"titleImage": "http://furcino.com/rest/images/sudoku.jpg"
}
When I return an object in the REST endpoint instead of a string and use the @Produces(MediaType.APPLICATION_JSON) annotation on the method, I get a JSON back. This JSON can be read by the frontend and used to display data.
Since I was writing multiple endpoints, I found it easier to put several annotations on each endpoint. The @Consumes annotation says, that the frontend should also send data in the JSON format. The @Tag annotation groups all endpoints with the same tag into one when generating frontend services. It is handy when you want all endpoints in a single file.
@Tag(name = "furcino-api")
@Path("/project")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProjectResource {
...
}
I am creating endpoints that deliver data. They are all structured. They have their path and they return specific kinds of objects. I need a way to access them with frontend code. This can be accomplished with an OpenAPI specification. It describes what kind of objects to send and receive on specific paths. The SmallRye OpenAPI extension lets me generate an OpenAPI specification and also test the endpoints. I can download the specification under http://localhost:8080/q/openapi and test my endpoints under http://localhost:8080/q/swagger-ui when running the server locally.
For angular, the specification can be processed with the following command. I replace the inputSpec with the downloaded specification and outputDir with a path to the output directory:
npx openapi-generator-cli generate -i inputSpec -g typescript-angular -o outputDir
It creates models of objects, which I receive and send in the frontend, and services to access endpoints.
I also needed a database to test my application with. In the beginning, the h2 database is easier to use, because it is in memory and I do not need to set up anything. You enable the database by writing the following into application.properties:
quarkus.datasource.db-kind=h2
quarkus.datasource.username=username-default
quarkus.datasource.jdbc.url=jdbc:h2:mem:default
quarkus.datasource.jdbc.max-size=13
It is also a good idea to create your database. This is achieved with the following line:
quarkus.hibernate-orm.database.generation=drop-and-create
This line can be also used with the final MySQL database (only the first time or the data gets lost) to avoid creating tables manually.
Locally, I use the quarkus server to serve the data. However, since I do not have hosting that supports Java I serve the data on my website with JSONs. For the following endpoint, I just create a file under ./article/pagination and call it by the number of the page. I also have to set the Access-Control-Allow-Origin header. I generate the files automatically from the data in the database.
@Tag(name = "furcino-api")
@Path("/article")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ArticleResource {
@GET
@Path("pagination/{page}")
@Schema(name = "getArticlesByPage")
public Response getArticlesByPage(@PathParam Long page) {
...
}
}