Spring Boot Rest
Stack Simple REST backend service. Spring Boot Unit Test 4/5 Mockito HSQL -> MYSQL Task Client can apply for loan asking for amount and term Loan application evaluation: Application is risky if: Loan application is done for max amount and loan application is created between 0:00–6:00 Client Applied 3 times using the same IP address

Stack
Simple REST backend service.
- Spring Boot
- Unit Test 4/5
- Mockito
- HSQL -> MYSQL
Task
- Client can apply for loan asking for amount and term
- Loan application evaluation: Application is risky if:
- Loan application is done for max amount and loan application is created between 0:00–6:00
- Client Applied 3 times using the same IP address
- Loan is granted if loan application goes through risk evaluation without errors.
- Once within loan term client can apply for term to be prolonged up to 14 days.
Planned
- Add Swagger to recognize this service https://swagger.io/resources/articles/best-practices-in-api-design/
- Add docker and deploy service to AWS from GitHub
- Add stemming libs from IoT .
Analyse
I assume that the risky loan request will be rejected.
Loan entity
static MAX_LOAN = 1000
– overrun
- loanId
Int
artifical id. - Amount
Long
- LoanName
String
- Ip
String
- TermAt
Date
- historyLog
String
– info about allowed operations. - CreatedAt
Date
- UpdatedAT
Date
example:
{ "id": 4, "amount": 999, "loanName": "IBM", "ip": "192.168.1.6", "termAt": "2020-02-22T16:01:08.756+0000", "historyLog": null, "createdAt": "2020-02-08T16:01:08.756+0000", "loanId": 4, "curentTime": "2020-02-08T16:01:08.900+0000", "prolongedTerm": false, "curentIp": "192.168.1.6" }
riskEvaluation
amount
> MAX_LOAN.createdAt
<0:00 – 6:00>.ip
same for 3 last apply in the sameloanName
.
UnitTest
- Take Loan < MAX_LOAN out of <00:00 – 06:00> – accepted.
- Take loan > MAX_LOAN – declined.
- Take loan from different IP.
- Take loan in different time <00:00 – 06:00>.
- TrolongTerm – check changes.
- Prolong two times loan – declined.
Impementation
- spring-boot-rest
- sample init data
HSQL
in import.sql - junit test
Setting environment (InteliJ)
- http://start.spring.io/ – pom.xml
-add-opens
and dependency for jaxb-api for suppres warning Illegal reflective access byorg.springframework.cglib.core.ReflectUtils$1
StackOverflowGROOVY_TURN_OFF_JAVA_WARNINGS=true
temp fixWARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1
- sample init data
HSQL
in import.sql.
REST APIs & logic
The app defines following simple CRU APIs. Without delete.
MAX_LOAN
= 1000 define loan overrun defined in entity model as column.
@Column
@NotNull(message = "Please enter correct amount of loan")
@Min(0)
@Max(MAX_LOAN) private long Amount;
Important case for variable entity ie nameLoan
, cratedAt
.
GET
http://localhost:8080 – show this renderedREADME.md
aboutAPI
.GET
http://localhost/api/loans – get alllonas
list (sample init data in import.sql)POST
http://localhost/api/loans – create newloan
example JSON:
{ "prolongedTerm": false, "loanName": "IBM", "ip": "127.0.0.1", "loanId": 1, "createdAt": "2020-02-06T22:19:44.000+0000", "termAt": "2020-03-06T22:19:44.000+0000", "amount": 999 }
Resposne
{ "id": 4, "amount": 999, "loanName": "IBM", "ip": "192.168.1.6", "termAt": "2020-02-22T16:01:08.756+0000", "historyLog": null, "createdAt": "2020-02-08T16:01:08.756+0000", "loanId": 4, "curentTime": "2020-02-08T16:01:08.900+0000", "prolongedTerm": false, "curentIp": "192.168.1.6" }
GET
http://localhost/api/loans/{loanId} – get id’s loan. Example http://localhost:8080/api/loans/1
{ "prolongedTerm": false, "loanName": "Cabacki", "ip": "127.0.0.1", "loanId": 1, "createdAt": "2020-02-06T22:19:44.000+0000", "termAt": "2020-03-06T22:19:44.000+0000" }
PUT
http://localhost/api/loans/{loanId} – not supportedPUT
http://localhost/api/loans/prolong/{loanId} – prolong loan 14dDELETE
http://localhost/api/loans/{loanId}
You can test them using postman (or any other rest client). Postman test collection: https://www.getpostman.com/collections/f4f319431e96e245d5a8
HSQL at mem
spring.datasource.url: jdbc:hsqldb:mem:restdb
Database Initialization
In a JPA-based applications, we can either choose to let Hibernate create the schema using entity classes or use schema.sql
, but we cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto
if using schema.sql
in application.properties
application.properties
spring.jpa.hibernate.ddl-auto=on
Sample init data HSQL
in import.sql.
drop table if exists loans CASCADE create table loans ( Id bigint generated by default as identity (start with 1), Amount varbinary(255) not null, CreatedAt timestamp not null, Ip varchar(255), LoanName varchar(20), ProlongedTerm boolean, TermAt timestamp, UpdatedAt timestamp, primary key (Id) )
Code Github
https://github.com/karol-preiskorn/LoanRESTHelp for Spring Boot REST
This module contains articles about Spring Boot RESTful APIs.
Relevant Articles
- HATEOAS for a Spring REST Service
- Versioning a REST API
- ETags for REST with Spring
- Testing REST with multiple MIME types
- Testing Web APIs with Postman Collections
- Spring Boot Consuming and Producing JSON
E-book
These articles are part of the Spring REST E-book:
- Bootstrap a Web Application with Spring 5
- Build a REST API with Spring and Java Config
- Http Message Converters with the Spring Framework
- Spring’s RequestBody and ResponseBody Annotations
- Entity To DTO Conversion for a Spring REST API
- Error Handling for REST with Spring
- REST API Discoverability and HATEOAS
- An Intro to Spring HATEOAS
- REST Pagination in Spring
- Test a REST API with Java