Monday, November 9, 2020

Json Restful API - Java Web With SQL Server

 


 

What is REST API / RESTful API ?

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services. REST stands for representational state transfer and was created by computer scientist Roy Fielding

Difference between API, REST API and RESTful API If you already know about APIs, maybe some of you still don't understand the difference between APIs, REST APIs, and RESTful APIs. Here's the difference: According to the explanation above, if the API is a software that integrates the applications that we make with other applications. The purpose of making it is to share data between applications that have been integrated. While the REST API is one of the architectural designs contained in the API itself. And the way the RESTful API works is that the REST client will access data/resources on the REST server where each resource is. Or the data/resource will be distinguished by a global ID or URIs (Universal Resource Identifiers). 

 So, later the data provided by the REST server can be in the form of text, JSON or XML format. And currently the most popular and most widely used format is the JSON format. The HTTP methods commonly used in REST api are: GET, serves to read data/resources from the REST server POST, serves to create a new data/resource on the REST server PUT, serves to update data/resources on the REST server DELETE, serves to delete data/resources from the REST serve OPTIONS, serves to get supported operations on resources from the REST server.

 

 Simple Project Web API with Java


 

A. Source SQL Connection 

DBSQLConnection.java
package com.aplikasi.db;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBSQLConnection {
    
    public Connection openConnection(){
        Connection Conn = null;        
        try {
            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/apps");
            Conn = ds.getConnection();
        }catch (Exception e) {
        }
        return Conn;
    }
}

 

B. Source Index json

Jsonapiapps.java

package com.aplikasi.apps;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.aplikasi.db.DBSQLConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@Path("/apps")
public class Jsonapiapps {
    /* FIND BY CUSTOMER - ID CUST */
    /*** url json api **/
    /** http://localhost:8080/JavaWebJsonAPI/v3/apps/Customer/A **/
    @GET
    @Path("/Customer/{CustID}")
    @Produces(MediaType.APPLICATION_JSON)
    public GetCustomerAPI RequestInquiry(@PathParam("CustID")String CustID)throws Exception {
        String SQL;
        String cCustName        = null;
        String cCustAddress        = null;
        Connection Conn = new DBSQLConnection().openConnection();
        Statement st = Conn.createStatement();   
        /** Connection Database */
        SQL = "SELECT * FROM CUSTOMER WHERE IDCUSTOMER = '"+CustID+"'";
        ResultSet rs = st.executeQuery(SQL);
        while(rs.next()){
             cCustName        = rs.getString("namecustomer");
             cCustAddress    = rs.getString("address");
        }
        rs.close();
        Conn.close();
        /**Get Modul GetCustomerAPI.java */
        GetCustomerAPI cust = new GetCustomerAPI();      
        cust .setCustomerID(CustID);
        cust .setCustomerName(cCustName);
        cust .setCustomerAddress(cCustAddress);
        return cust;
    }
}
 

 

C. Source Module GetCustomer

GetCustomerAPI.java
package com.aplikasi.apps;

public class GetCustomerAPI {

    private String CustomerID;
    private String CustomerName;
    private String CustomerAddress;
    

    /* Customer ID */
    public String getCustomerID() {
        return CustomerID;
    }
    public void setCustomerID(String CustomerID) {
        this.CustomerID = CustomerID;
    }

    /* Customer Name */
    public String getCustomerName() {
        return CustomerName;
    }
    public void setCustomerName(String CustomerName) {
        this.CustomerName = CustomerName;
    }
    
    /* Customer Address */
    public String getCustomerAddress() {
        return CustomerAddress;
    }
    public void setCustomerAddress(String CustomerAddress) {
        this.CustomerAddress = CustomerAddress;
    }

    @Override
    public String toString() {
        return getCustomerID().toString()+ "," + getCustomerName().toString()+ "," + getCustomerAddress().toString();

    }    
    
    public GetCustomerAPI( String CustID,String CustName,String CustAddress) {
        super();
        this.CustomerID         = CustID;
        this.CustomerName         = CustName;
        this.CustomerAddress    = CustAddress;
    }

    public GetCustomerAPI() {        
        super();
    }
    
}

D. Running Web

    Open Browser type url :  localhost:8080/JavaWebJsonAPI/v3/apps/Customer/A

    


E. Create Database SQL Server (Tabel Customer)


F. Tutorial Youtube



 

 

 

 

No comments:

Post a Comment

Free Templates Source Code Simple Project Java ZK Framework

  Free Templates Source Code Simple Project Java ZK Framework   ZK Framework aims to combine the simplicity and security from its server-cen...