Code Monkey home page Code Monkey logo

customerapi's Introduction

CustomerAPI

CRUD operations REST API implemented in Spring Boot performed using Oracle Database - 18.3.0.0.0

Implementation steps-

  1. Create a maven project and add below parent pom dependency in the pom.xml file to make our project as a child of it. This dependency includes the default configuration and provides plugin management for applications built with Maven.

        <parent>
     	<groupId>org.springframework.boot</groupId>
     	<artifactId>spring-boot-starter-parent</artifactId>
     	<version>1.4.2.RELEASE</version>
        </parent>
    
  2. Add below properties (it is optional) in pom.xml to support java version 8.

        <properties>
     	<java.version>1.8</java.version>
        </properties>
    
  3. We want to create a REST API and this needs below dependency to be added in the pom.xml file. This dependency will inject all the jars required to use the annotations for creating a REST API. For example, @RestController, @RequestMapping etc.

     <dependencies>
     	<dependency>
     		<groupId>org.springframework.boot</groupId>
     		<artifactId>spring-boot-starter-web</artifactId>
     	</dependency>
     </dependencies>
    
  4. Add below dependencies to make oracle db connection using JDBC.

     <dependency>
     	<groupId>org.springframework.boot</groupId>
     	<artifactId>spring-boot-starter-data-jpa</artifactId>
     </dependency>
    
     <dependency>
     	<groupId>com.oracle</groupId>
     	<artifactId>oracle</artifactId>
     	<version>18.3.0.0.0</version>
     </dependency>
    

You might get an error "Missing artifact com.oracle:oracle:jar:18.3.0.0.0"

Download ojdbc8.jar explicitly and place it in a folder, run below command in the folder containing the jar and then you should get a BUILD SUCCESS message.

mvn install:install-file -Dfile=ojdbc8.jar -DgroupId=com.oracle -DartifactId=oracle -Dversion=18.3.0.0.0 -Dpackaging=jar -DgeneratePom=true

This will inject oracle-18.3.0.0.0.jar in the project's classpath required to make oracle database connection. This step is to be done because the jar is not yet available on maven central repository.

Update the maven project once and you will see the error is gone and oracle-18.3.0.0.0.jar will be added to the Maven dependencies section.

  1. Create a new configuration file application.yml in src/main/resources folder and add below properties.

     #tomcat server port
     server:
       port: 9090
    
     ---
    
     #database config
     spring: 
          datasource:
     	url: jdbc:oracle:thin:@localhost:1521/orclpdb
     	username: <<your username>>
     	password: <<your password>>
     	driver-class-name: oracle.jdbc.OracleDriver
    
  2. Login to your oracle db schema using sqlplus or any oracle IDE like SQlDeveloper. Create customers table and customer_seq as below. customers table is used for storing customer data and customer_seq is used for

     --Customers table
         CREATE TABLE CUSTOMERS(
        id INTEGER NOT NULL PRIMARY KEY,
        firstname VARCHAR2(20) NOT NULL,
        lastname VARCHAR2(20) NOT NULL,
        age INTEGER NOT NULL,
        emailid VARCHAR2(20) NOT NULL
     );
    
     --customer sequence
     CREATE SEQUENCE customer_seq
     MINVALUE 1
     MAXVALUE 99999999
     START WITH 1
     INCREMENT BY 1
     CACHE 10;
    
  3. REST API endpoints for customer -

    a. Adding a new customer -

     @RequestMapping(value = "/customers/add", method = RequestMethod.POST)
    

    b. Retrieving a customer based on its id -

     @RequestMapping(value = "/customers/{id}", method = RequestMethod.GET)
    

    c. Deleting a customer -

     @RequestMapping(value = "/customers/{id}", method = RequestMethod.DELETE)
    

    d. Updating existing customer details -

     @RequestMapping(value = "/customers/{id}", method = RequestMethod.PUT)
    

customerapi's People

Contributors

hetalrachh avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.