Spring Boot Rest
EN-US: Build a Spring Boot loan API with risk eval, Mockito & HSQL/MySQL. PL: Zbuduj API pożyczkowe w Spring Boot z analizą ryzyka, Mockito, HSQL/MySQL.

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
Intartifical id. - Amount
Long - LoanName
String - Ip
String - TermAt
Date - historyLog
String– info about allowed operations. - CreatedAt
Date - UpdatedAT
Date
example:
riskEvaluation
amount> MAX_LOAN.createdAt<0:00 – 6:00>.ipsame 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
HSQLin import.sql - junit test
Setting environment (InteliJ)
- http://start.spring.io/ – pom.xml
-add-opensand dependency for jaxb-api for suppres warning Illegal reflective access byorg.springframework.cglib.core.ReflectUtils$1StackOverflowGROOVY_TURN_OFF_JAVA_WARNINGS=truetemp fixWARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1- sample init data
HSQLin 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.
GEThttp://localhost:8080 – show this renderedREADME.mdaboutAPI.GEThttp://localhost/api/loans – get alllonaslist (sample init data in import.sql)POSThttp://localhost/api/loans – create newloanexample JSON:
Resposne
GEThttp://localhost/api/loans/ – get id's loan. Example http://localhost:8080/api/loans/1
PUThttp://localhost/api/loans/ – not supportedPUThttp://localhost/api/loans/prolong/ – prolong loan 14dDELETEhttp://localhost/api/loans/
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
[simple_icon name="github"] https://github.com/karol-preiskorn/LoanREST
Help 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
Spring Boot Rest it is simple very easy application.