Monday, July 1, 2019

Tuto Spring Boot Webservice / JPA Repository / Oracle / Maven / Eclipse

This tutorial aims to build a simple web service using spring boot .
Let's start by ddownload our start package from :
https://start.spring.io/

We will call our project solvability <=> Artifact a sub functionnality of an extranet web services portal tn.cnss.extranet.employeur <=>Group
We will also choose maven to manage dependencies, tests and continuous dev/deployement actions




fig 1- Starting springboot package


Now w will import our project in eclipse environnement :
File > Import maven project



fig 2- Import project in eclipse



fig3- Before maven configuration

Once the project was imported, we will configure maven configuration in eclipse by indicating our M2_HOME
by default maven is configured to store dependencies on /user_home/.m2/repository as shown in this image


And after modification:


fig4- After maven changes

Now we will add our unique additional dependency (oracle driver)
Here ive already in my repo oracle 11.2.0.4 driver
For those who don't have oracle driver installed :
 1- Start by downloading the ojdbc (or copy it from sqldeveloper tool if you use it)
2- run this command :
mvn install:install-file -Dfile=<path-to-oracle-driver-file>

Once all is done we add the dependency tag in our pom file

        <dependency>

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc</artifactId>

            <version>11.2.0.4</version>

        </dependency>



fig5- Oracle dependency






fig6- application.properties file


Now let's continue:
We have to setup our /src/main/resources/application.properties file; and put some needed parmaters:
in our case:
spring.datasource.url=jdbc\:oracle\:thin\:@ip.ip.ip.ip:\port\:SID
spring.datasource.username=user_name
spring.datasource.password=user_password
spring.jpa.properties.hibernate.show_sql=true_or_false
spring.jpa.properties.hibernate.use_sql_comments=true_or_false
spring.jpa.properties.hibernate.format_sql=true_or_false
server.port = our_webservice_port_number
logging.level.org.hibernate=ERROR_see_hibernate_logging_level
NB: ERROR is less verbose



Note now our application can start without errors.

Let's continue
Now we will create our RestController (Entry points for remote invokations)
A RestController is a java class annotated with @RestController
(Reference to org.springframework.web.bind.annotation.RestController)

To make our code structured we can create a package controller under our project






fig7- controllers package



And then we create our controller  under the new created  package
In our case we will check the solvability of a customer we will need two params
root number and key (consistency)
we will invoke that method from "APP_URL/check-solvability " with simple http GET request

So we use
@GetMapping annotation

So the behavior of our code  could be
 /**
  * Check if customer is solvable or no
  * @param root
  * @param key
  * @return true or no
  */
 @GetMapping( "/check-solvability/{rootValue}/{keyValue}") 
 public String getCustomerFinancialSituation(@PathVariable String rootValue, @PathVariable String keyValue ){
  //Some logic here 
  //For exemple if we will return true always
   JSONObject output = new JSONObject();
   output.put("result", "ok");
   output.put("value","1");
   return output.toString();
 
 }

Let's instroduce a business layer (it's logic that the business rules are delegated to a seperated class  
And we create our business layer or business package 
and business implementations package

fig8- business package

And we create this interface that will be implemented by the financial dev team 


fig9- business interface

Let's continue
Once interface is created we can proceed to business injection in the main controller, this can be done by these two lines
 @Autowired
 private FinancialBusiness financialBusiness;

fig10- business injection


Lets's continue

Now we create implementation package for business, and a specific implementation for our previously ceated interface Financial business




fig11- business interface implementation


if you want to interoduce repositories cantact me for more advanced example.








No comments:

Post a Comment