#Run mysql docker container
$ docker run --detach --name mysql-api-demo -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=ApiDemo -p 3306:3306 mysql:8.1.0
#Connect to bash in container
$ docker exec -it mysql-api-demo mysql -u root -ppassword
#Create new user
MYSQL> CREATE USER 'TEST'@'%' IDENTIFIED BY 'password';
#Grant rights to new user
MYSQL> GRANT ALL ON `ApiDemo`.* TO 'TEST'@'%';
MYSQL> FLUSH PRIVILEGES;
#Exit mysql
MYSQL> exit
#Check if logging into new user works
$ docker exec -it mysql-api-demo mysql -u TEST -ppassword
(config data will also be inserted in the database)
$ cd flyway-db
#Clean database
$ mvn flyway:clean
#Migrate database
$ mvn flyway:migrate
#Add datasource to IntelliJ
Host: localhost
Port: 3306
Authentication: User & Password
User: TEST
Password: password
URL should be: jdbc:mysql://localhost:3306/ApiDemo
$ cd /home/.../tools/wildfly-33.0.0.Final/bin/
#Linux
$ sh jboss-cli.sh --connect
#Windows
> .\jboss-cli.bat --connect
[standalone@localhost:9990 /] module add --name=com.mysql --resources=/home/.../api-demo/src/main/resources/wildfly/modules/com/mysql/main/mysql-connector-j-9.0.0.jar --dependencies=javax.api,javax.transaction.api
[standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-name=mysql,driver-module-name=com.mysql,driver-xa-datasource-class-name=com.mysql.cj.jdbc.MysqlXADataSource)
[standalone@localhost:9990 /] data-source add --name=ApiDemoPersistenceDS --connection-url=jdbc:mysql://localhost:3306/ApiDemo --jndi-name=java:jboss/datasources/ApiDemoPersistenceDS --driver-name=mysql --user-name=TEST --password=password --transaction-isolation=TRANSACTION_READ_COMMITTED --min-pool-size=10 --max-pool-size=50 --pool-prefill=true --allocation-retry=3 --allocation-retry-wait-millis=100 --valid-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker --validate-on-match=false --background-validation=true --background-validation-millis=30000 --stale-connection-checker-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLStaleConnectionChecker --exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter --enabled=true
[standalone@localhost:9990 /] /extension=org.wildfly.extension.microprofile.openapi-smallrye:add()
[standalone@localhost:9990 /] /subsystem=microprofile-openapi-smallrye:add()
Now you can start the WildFly server.
-
Your API is callable under this URL: http://localhost:8080/apiDemoWar-1.0-SNAPSHOT/api/hello-world
-
And if you want to see all Endpoint go to the SWAGGER UI: http://localhost:8080/apiDemoWar-1.0-SNAPSHOT/api/openapi-ui
#Pull repository
$ git pull https://github.com/marcel-gepardec/api-demo.git
#Replace configuration for mysql database in the file docker-compose.yml
environment: # Set Passwords, User and Database
MYSQL_ROOT_PASSWORD: password # Set the root password for MySQL
MYSQL_DATABASE: ApiDemo # Create a database named 'ApiDemo'
MYSQL_USER: DB_USER_NAME # Create a user with the specified username
MYSQL_PASSWORD: DB_USER_PASSWORD # Set the password for the user
#Attention!!! If you use Linux/ARM64, you need to add this to your /etc/hosts file
$ echo "172.17.0.1 host.docker.internal" | sudo tee -a /etc/hosts > /dev/null
#Run docker compose file
$ docker compose up -d