Simple jersey 2 start application with president example database - used for Project at Fontys Venlo.
This example is just a start, this means, no detailed explanations will be given as well as no ready implementation. It is also not mandatory to do it like this.
The structure is as follows:
- You use a docker container for the database. The same thing as last semester, but now we will create an own docker container for that based on the postgres container.
- why? Because we want to have the database be filled already with our testdata, in this case the president database. Nice, right?
- Then you run the application locally via jetty on your machine and this connect with the postgres database on your docker container
- clone first (if you want to use it as base for your project, use this as a template, see button above near cloning button)
- build your own docker container based on the Dockerfile. Have a look into it how it works.
docker build -t mydbimage:0.1 .
- you now created an own image which is installed locally on your machine. Look it up by typing
docker images
in your terminal. You should see it. - Now run your own image:
docker run -d -p 5432:5432 --name myprj2database mydbimage:0.1
-
Default username is
postgres
-
Default password is
mypassword
-
Default database is
postgres
(here all the tables will be in) -
now start this sample application locally with maven:
mvn jetty:run
-
then goto http://localhost:8080/webapi/myresource/presidents/1
Et voilà, you have a running REST-Api Backend-example.
You can take this as a starting point for your project 2.
- You can use this as base for your own application. Note that you should put your SQL files into sql-files folder, have a look into the already existing files (which are from databases 1 course). The databae will be created with your schema and filled with your data.
- If you change your pre-filled database sql files, you need to (re)-create your database image and e.g. give it a new version, e.g.:
docker build -t mydbimage:0.2 .
You can choose freely how you name you images (e.g. mydbimage or superimage) and the version tags (e.g. 0.1, 0.2, 1.0... ) Advise is to stick to normal version numbers.
-
The java application has already the correct connection parameters set up for connecting with the database, but only if you did everything like described above. If you change the passwords in the Dockerfile, of course you need to adapt it.
-
Note: it could be that you have to clean your application
mvn clean
or usemvn clean jetty:run
Have fun and feel free to change for your project purpose
- if you understand fully what was done here, you can deploy as well the java application as war to docker if you like.
- further, you could use Docker compose for running all services with one command.