This example relies on the SpringBoot to create a REST API and connecting to a MySQL database using JPA, with a sample web application and a command line client.
The basic structure of a SpringBoot project can be initialized using Spring initializr at https://start.spring.io/.
First, check all the required dependencies specified by the pom.xml file and the database configuration contained in src/main/resources/application.properties file.
Now, run the following command to check to download all dependencies and check that everything correctly compiles
mvn compile
Make sure that the MySQL database was correctly configured before connecting the application to use it. Use the contents of the file src/main/resources/dbsetup.sql to create the database, an specific user 'spq' (pass: 'spq') for the application and grant privileges to that user. If you are using the command line client for MySQL you could run the following command
mysql –uroot -p < src/main/resources/dbsetup.sql
otherwise you could use the contents of the file in any other MySQL client you are using.
Now, launch the server using the following command
mvn spring-boot:run
If there are no errors, you should get a running web application serving its contents at http://localhost:8080/. You can press Ctrl+C to stop the running application.
The application exposes a REST API, which is used by the web application, which is implemented in the BookController class. To execute these methods, you may install POSTman app or CURL command line tool. For example, some methods are
Retrieves all the registered books
GET http://localhost:8080/api/books
Adds a new book to the database
POST http://localhost:8080/api/books
Content-Type: application/json
{
"title": "Spring Boot in Action",
"author": "Craig Walls",
"isbn": "9781617292545"
}
Removes a previously registered book
DELETE http://localhost:8080/api/books/1
To see the full list of methods from the REST API, you can visit Swagger interface at: http://localhost:8080/swagger-ui.html. Check the annotations in the BookController class, the required dependencies in the pom.xml file and the application.properties file for its configuration
There is a sample REST API client implementation using the SpringBoot REST client libraries in class BookManager.java. You can launch the client using the following Maven command (check)
mvn exec:java
See section in pom.xml to see how this command was configured to work.
Application can be packaged executing the following command
mvn package
including all the SpringBoot required libraries inside the target/rest-api-0.0.1-SNAPSHOT.jar, which can be distributed.
Once packaged, the server can be launched with
java -jar rest-api-0.0.1-SNAPSHOT.jar
and the sample client by running, as SpringBoot changes the way the default Java loader
java -cp rest-api-0.0.1-SNAPSHOT.jar -Dloader.main=com.example.restapi.client.BookManager org.springframework.boot.loader.launch.PropertiesLauncher localhost 8080
Therefore, in a real development, it would be advisable to create different Maven projects for server and client applications, easing distribution and manteinance of each application separately.
In SpringBoot, performance metrics can be enabled. For that:
- Declare the following dependency in
pom.xml
:<!-- Access metrics at: http://localhost:8080/actuator/metrics --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- Add the following lines to
application.properties
configuration file:# Enable metrics management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always management.endpoint.metrics.enabled=true
Metrics endpoint: 👉 http://localhost:8080/actuator/metrics
Example sub-metric (e.g. JVM memory): 👉 http://localhost:8080/actuator/metrics/jvm.memory.used
Check the documentation about how to use a Java Profile, e.g. VisualVM Java profiler.
- Very good explaination of the project: https://medium.com/@pratik.941/building-rest-api-using-spring-boot-a-comprehensive-guide-3e9b6d7a8951
- Building REST services with Spring: https://spring.io/guides/tutorials/rest
- Good example documenting how to generate Swagger APIs in Spring Boot: https://bell-sw.com/blog/documenting-rest-api-with-swagger-in-spring-boot-3/#mcetoc_1heq9ft3o1v
- Docker example with Spring: https://medium.com/@yunuseulucay/end-to-end-spring-boot-with-mysql-and-docker-2c42a6e036c0