This is project is a collection of tutorials to learn about creating a RESTful web services using Spring Boot, Maven, and PostgreSQL. Start with Tutorial 1 to familiarize yourself with Spring Boot. Then, move on to Tutorial 2 to learn about RESTful web services.
https://spring.io/guides/gs/rest-service/
Note: use Maven (not Gradle)
Tutorial Purpose: The purpose of this tutorial is to demonstrate how to create a basic RESTful web service using Spring Boot, Maven, and PostgreSQL. The four major REST operations are demonstrated (GET, POST, PUT, DELETE). This tutorial intentionally excludes detailed steps on configuring your STS and PostgreSQL environments, as these steps are already well documented in numerous other resources.
- Install PostgreSQL
- Create new PostgreSQL database named 'CarApp' (username = postgres, Password = demoPass)
- Initialize the schema by executing the script located at:
https://github.com/devmjm/spring-rest-service-tutorial/blob/master/Scripts/CreateAndPopulateCarappDb
- Clone the CarAppPartial repository from https://github.com/devmjm/spring-rest-service-tutorial to your STS workspace
- Open the CarApp project in STS
- Run the CarApp project as a 'Spring Boot App' and confirm it compiles and runs
- Stop the project
- Review the "REST Basics" resource below to familiarize yourself with REST
- Navigate to the CarController. Note that there are GET, POST, PUT, and DELETE methods outlined.
- Fill in the logic for these methods in accordance with RESTful standards (http://www.restapitutorial.com/lessons/httpmethods.html). Your methods should return the following responses:
VERB | URI | Request Body | Expected Response |
---|---|---|---|
GET | localhost:8080/cars | N/A | HttpStatus OK (200). List of all cars in your database. |
GET | localhost:8080/cars/{vin} | N/A | HttpStatus OK (200). Car with vin is returned |
POST | localhost:8080/cars/ | { "Vin": 9, "Driver": "fb35753c-668c-11e7-ae07-d790c545bb70" } | HttpStatus Created (201). Car is created |
DELETE | localhost:8080/cars/{vin} | N/A | HttpStatus OK (200). Car is deleted. |
PUT | localhost:8080/cars/{vin} | { "Vin": 111, "Driver": "fb35753c-668c-11e7-ae07-d790c545bb70" } | HttpStatus OK (200). Car is updated. |
- Run the project as a 'Spring Boot App'
- Use Postman to make REST requests to your service
- Confirm you are able to achieve the behavior described in step 3 above.
A completed version of this demo can be found at: https://github.com/devmjm/spring-rest-service-tutorial-complete
Tutorial Purpose: The purpose of this tutorial is to get an introduction to Azure. You will deploy the CarApp you built in Tutorial 2 as an Azure Web Service. Instead of using your local PostgreSQL instance, you will point your application to a pre-configured Azure SQL database.
- Replace your application.properties file with the below values. Ask a team member to provide the username and password for the database.
spring.datasource.url=jdbc:sqlserver://fuller-sql-dev1.database.windows.net;databaseName=FULLER_SQL_DEV01
spring.datasource.username= {db username}
spring.datasource.password= {db password}
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto=validate
- In the
pom.xml
file, remove the dependency for PostgreSQL. - Add the dependency below for Azure SQL Server:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.1.jre8</version>
</dependency>
To deploy the CarApp as an Azure Web Service, follow the steps outlined here: https://docs.microsoft.com/en-us/azure/app-service/app-service-deploy-spring-boot-web-app-on-azure
Note: for this demo, please use the "Free Tier" Azure service. This will be adequate for the purpose of the demo.
Test REST endpoints outlined specified in the "Start Building the REST Service" section of Tutorial 2. In the request URLs, replace localhost:8080
with the domain of your web application.
- REST basics: https://spring.io/understanding/REST
- REST Standards: http://www.restapitutorial.com/lessons/httpmethods.html
- Advanced REST tutorial: https://spring.io/guides/tutorials/bookmarks/
- Deploy Spring Boot app to Azure: https://docs.microsoft.com/en-us/azure/app-service/app-service-deploy-spring-boot-web-app-on-azure