Skip to content

dipina/SpringBootBookRESTService

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpringBoot + REST API + MySQL

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/.

Launching the application

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.

REST API

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

Command line client

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.

Packaging the application

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.

Gathering performance metrics

In SpringBoot, performance metrics can be enabled. For that:

  1. 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>
  2. Add the following lines to application.propertiesconfiguration 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.

References

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published