BeAPI Framework Installation
Getting started with the BeAPI Springboot Starter is a simple process. Prior to building and running though, we must first talk installation.
Requirements
The BeAPI™ API Framework has the following project requirements:
- Java 1.8 >
- Springboot 2.6.2 >
- Gradle 7 > (for building)
Start by cloning the project into your local directory:
git clone https://github.com/orubel/spring-boot-starter-beapi.git
Configure Demo Project
You will also need to have your configuration setup in your home directory (or the directory of the user running the app):
Use the following command in a shell to create your local config directory :
git clone https://github.com/orubel/spring-boot-starter-beapi-config.git ~/.boot
Now go to the directory '~/.boot/{env}/' and copy a file called 'beapi_api.yaml' to your '{beapi-java-demo}/src/main/resources' directory :
cd ~/.boot cp beapi_api.yaml location/of/demo-application/src/main/resources
Note:
We highly suggest using the Beapi-Java-Demo as it acts as a SDK as well, making full use of the project and security.Now you are setup and can start configuring your project...
Make sure to follow these steps as the project will NOT BUILD if you do not have the datasource and the DB setup.
Setup Demo Project DB
Downloaded with the config was a file called boot.sql. This will allow you to setup your default DB for the demo project.
- Login to MySQL via the shell from the directory in which the 'boot.sql' file is and use the following commands:
mysql -uroot -p{yourpassword} create database boot; use boot; source boot.sql; GRANT ALL ON boot.* to 'yourlogin'@'localhost' IDENTIFIED BY 'yourpassword'; FLUSH PRIVILEGES; exit;
- Next, go into your ~/.boot/dev directory and find the beapi_db.yaml file for editing and change the following lines:
username: "root" password: "password"
NOTE: If you want to test in other environments, you will have to copy this file to the 'test' and 'prod' folders as well.
- Finally, go into your 'demo-application/src/main/resources/application.properties' and make sure, that your environment is set to 'dev':
spring.profiles.active=dev
Build Demo Project
After you have properly configured your project, your next step is to run the project. If you have not yet cloned the application, do so now in the directory of your choice (preferable one owned by the same user where you installed the config files):
- Use the following command in a shell to clone the project :
git clone https://github.com/orubel/spring-boot-starter-beapi.git
- Now cd into the project :
cd spring-boot-starter-beapi
- ... and run the following :
gradle clean;gradle build
- If all goes well, cd into the demo project and run the following :
cd demo-application java -jar build/libs/demo-application-{version}.jar
Run Demo Project
If your demo project is properly running, you should now be able to get a token and starts making requests.
- First, look in your demo-application/src/main/resources dir for a file called 'beapi_api.yaml'. In this file you will find the bootstrapping for an ADMIN and a TEST user that are created on startup :
bootstrap: superUser: { login: 'admin', password: '@6m!nP@s5', email: 'email@yourdomain.com' } testUser: { login: 'test', password: 't35tP@s5', email: 'test@yourdomain.com', }
- Use the superUser login/password to create a curl request to get your token:
curl -v -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"@6m!nP@s5"}' http://localhost:8080/authenticate
- This will return something that looks like the following :
> GET /v0.4.1/user/show/test HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.58.0 > Accept: */* > Content-Type: application/json > Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY2MDA4NzM1OSwiaWF0IjoxNjYwMDY5MzU5fQ.bR8lgrGlzCmHKhXC1D_LF-vVmFINAVX9kgA2n-EiIbslYaix65FXB7qrPRVJxrgqhzNcdTiBOhScbujQwy0ufA
- Now take everything after 'Authorization' (not the space of course) and add it to your 'authorization' header on an api call:
curl -v -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY2MDA4NzM1OSwiaWF0IjoxNjYwMDY5MzU5fQ.bR8lgrGlzCmHKhXC1D_LF-vVmFINAVX9kgA2n-EiIbslYaix65FXB7qrPRVJxrgqhzNcdTiBOhScbujQwy0ufA" --request GET "http://localhost:8080/v0.4.1/user/show/test"
- Voila! If all goes well, this will output the 'test' user that was bootstrapped on startup:
{"firstName":"null","lastName":"null","accountExpired":false,"id":20,"version":2229,"email":"test@yourdomain.com","enabled":true,"username":"test"}
Troubleshooting
- Make sure you have proper READ/WRITE/EXECUTE(755) for the files in ~/.boot/ and ~/.boot/.iostate
- Make sure you have matching named Controllers for all IO State files; see examples in ~/.boot/.iostate directory
- If you want to use a version of Java HIGHER than 1.8, change 'sourceCompatibility' in ALL gradle.properties files (ie demo-application/gradle.properties,/gradle.properties)