Friday, 15 July 2022

Standard for Restfull API


 
The concept of REST is to separate the API structure into logical resources. There are used the HTTP methods GET, DELETE, POST and PUT to operate with the resources.
These are some best practices to design a clean RESTful API:

1. Use nouns but no verbs-

For an easy understanding use this structure for every resource:
Do not use verbs:
/getAllCars
/createNewCar
/deleteAllRedCars

2. GET method and query parameters should not alter the state-

Use PUT, POST and DELETE methods  instead of the GET method to alter the state. Do not use GET for state changes:
GET /users/711?activate or
GET /users/711/activate

3. Use plural nouns-

Do not mix up singular and plural nouns. Keep it simple and use only plural nouns for all resources.
/cars instead of /car
/users instead of /user
/products instead of /product
/settings instead of /setting

4. Use sub-resources for relations-

If a resource is related to another resource use subresources.
GET /cars/711/drivers/ Returns a list of drivers for car 711
GET /cars/711/drivers/4 Returns driver #4 for car 711

5. Use HTTP headers for serialization formats-

Both, client and server, need to know which format is used for the communication. The format has to be specified in the HTTP-Header.
Content-Type defines the request format.
Accept defines a list of acceptable response formats.

6. Provide filtering, sorting, field selection and paging for collections-

Filtering:
Use a unique query parameter for all fields or a query language for filtering.
GET /cars?color=red Returns a list of red cars
GET /cars?seats<=2 Returns a list of cars with a maximum of 2 seats
Sorting:
Allow ascending and descending sorting over multiple fields.
GET /cars?sort=-manufactorer,+model
This returns a list of cars sorted by descending manufacturers and ascending models.
Field selection:
They don’t need all attributes of a resource. Give the API consumer the ability to choose returned fields. This will also reduce the network traffic and speed up the usage of the API.
GET /cars?fields=manufacturer,model,id,color
Paging:
Use limit and offset. It is flexible for the user and common in leading databases. The default should be limit=20 and offset=0
GET /cars?offset=10&limit=5
To send the total entries back to the user use the custom HTTP header: X-Total-Count.
Links to the next or previous page should be provided in the HTTP header link as well. It is important to follow this link header values instead of constructing your own URLs.

7. Handle Errors with HTTP status codes-

It is hard to work with an API that ignores error handling. Pure returning of a HTTP 500 with a stacktrace is not very helpful.
Use HTTP status codes
The HTTP standard provides over 70 status codes to describe the return values. We don’t need them all, but  there should be used at least a mount of 10.
200 – OK – Eyerything is working
201 – OK – New resource has been created
204 – OK – The resource was successfully deleted
304 – Not Modified – The client can use cached data
400 – Bad Request – The request was invalid or cannot be served. The exact error should be explained in the error payload. E.g. „The JSON is not valid“
401 – Unauthorized – The request requires an user authentication
403 – Forbidden – The server understood the request, but is refusing it or the access is not allowed.
404 – Not found – There is no resource behind the URI.
422 – Unprocessable Entity – Should be used if the server cannot process the enitity, e.g. if an image cannot be formatted or mandatory fields are missing in the payload.
500 – Internal Server Error – API developers should avoid this error. If an error occurs in the global catch blog, the stracktrace should be logged and not returned as response.

 
Happy Coding... :) 

Sunday, 16 May 2021

Where are Docker images stored on the host machine?

The contents of the /var/lib/docker directory vary depending on the driver Docker is using for storage.

By default this will be aufs but can fall back to overlay, overlay2, btrfs, devicemapper or zfs depending on your kernel support. In most places this will be aufs but the RedHats went with devicemapper.

You can manually set the storage driver with the -s or --storage-driver= option to the Docker daemon.

  • /var/lib/docker/{driver-name} will contain the driver specific storage for contents of the images.
  • /var/lib/docker/graph/<id> now only contains metadata about the image, in the json and layersize files.

In the case of aufs:

  • /var/lib/docker/aufs/diff/<id> has the file contents of the images.
  • /var/lib/docker/repositories-aufs is a JSON file containing local image information. This can be viewed with the command docker images.

In the case of devicemapper:

  • /var/lib/docker/devicemapper/devicemapper/data stores the images
  • /var/lib/docker/devicemapper/devicemapper/metadata the metadata
  • Note these files are thin provisioned "sparse" files so aren't as big as they seem.  
 
This answer from stack-overflow.
 
 
Happy learning :) 

Saturday, 8 May 2021

What is Docker, learn with simple slides

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.


Enjoy the slide Please let me know if you have any query.

Java Interview Question

What is transient variable?

Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.

What do you understand by Synchronization?

Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.
E.g. Synchronizing a function:

public synchronized void Method1 () {
// Appropriate method-related code.
 
}
E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
 

// Synchronized code here.
}
}



What is Collection API?

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
 
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

Explain the Encapsulation principle.

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
 


Explain the different forms of Polymorphism.
From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:
 
  • Method overloading 
  • Method overriding through inheritance 
  • Method overriding through the Java interface 
 
 
 
Happy Learning .. Wait for more I will added soon..... :)

Tuesday, 21 April 2020

How to Generate JAR and Use as dependency in Spring Boot

For Generating Jar File-

You can extend your project by maven-assembly-plugin.

What is Assembly Plugin-

An "assembly" is a group of files, directories, and dependencies that are assembled into an archive format and distributed. For example, assume that a Maven project defines a single JAR artifact that contains both a simple application and a web application. Such a project could define two "assemblies" that bundle the application with a different set of supporting scripts and dependency sets. One assembly would be the assembly for the simple application, and the other assembly could be a web application bundled with a slightly different set of dependencies. for more about assembly plugin click here

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
        <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        </configuration>
        <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
            <goal>single</goal>
            </goals>
            </execution>
        </executions>
</plugin>

Include these line in your pom.xml and make build of it by using maven command -

mvn package

After the build you will get 3 jars. The main one will be the same as a usual Maven project, while the second one will have the classifier appended with exec and be the executable JAR. The third jar name will be appended by jar-with-dependencies and will contain your classes with classes added as dependencies in your spring boot application(spring-boot-starter-web, thymeleaf,...), so into the pom of the application where you want to add that project as dependencies you won't have to add dependencies from spring boot project.

Include Generated Jar In your another project -

Steps for adding external jars in IntelliJ IDEA:

  1. Click File from the toolbar
  2. Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
  3. Select Modules at the left panel
  4. Dependencies tab
  5. '+' → JARs or directories
Happy Coding  

Monday, 4 September 2017

Artificial Intelligence (AI)

Explanation

Definition - What does Artificial Intelligence (AI) mean?

Artificial intelligence (AI) is an area of computer science that emphasizes the creation of intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include speech recognition, learning, planning and problem solving.

Sunday, 3 September 2017

What is Angular?




Intent

Learn one way to build applications with Angular and reuse your code and abilities to build apps for any deployment target. For web, mobile web, native mobile and native desktop.

Saturday, 2 September 2017

what are Java features ?

Java 5 Features:


    > For-each loop
    > Autoboxing and Unboxing
    > Covariant Return Type
    > Annotation
    > Generics
    > Enum
    > Static Import
    > Varargs
    

Friday, 1 September 2017

Difference between ear jar and war files ?

These files are simply zipped files using java jar tool. These files are created for different purposes. 


.jar files: The .jar files contain the group of .class files and libraries. 


.war files: The war file contains static resources i.e., html, js, css...etc and dynamic resources jsp, servlets etc and other files (web.xml) for necessary for the development of web applications. Combination of these files zipped in to one module called .War file. 


.ear files: The .ear file contains anything Java/J2EE (Enterprise) related files zipped in to one module called .ear file. It is deployed in Application server.

How to Encode/Decode URL Online

What is URL Encoding/Decoding?

URL encoding stands for encoding certain characters in a URL by replacing them with one or more character that consist of the percent character “%“ followed by 2 hexadecimal digits. So URL encoding and decoding is used that a string conform to the the Uniform Resource Locators Specification – RFC 1738.

Online Websites for Encoding / Decoding URL: