Wednesday, January 24, 2018

Spring BOOT

Spring BOOT: Spring Boot is an open-source Java-based framework used to create a Micro Service.It explores major features of Spring Boot such as 
  • Starters, 
  • Auto-configuration, 
  • Beans, 
  • Actuator and more


Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring application that you can just run

Advantages

Spring Boot offers the following advantages to its developers −

  • Easy to understand and develop spring applications
  • Increases productivity
  • Reduces the development time

Goals

Spring Boot is designed with the following goals −

  • To avoid complex XML configuration in Spring
  • To develop a production ready Spring applications in an easier way
  • To reduce the development time and run the application independently
  • Offer an easier way of getting started with the application
Starters
spring-boot-starter-actuator : monitor and manage your application.
spring-boot-starter-data-jpa : Spring and JPA for database access
spring-boot-starter-security : monitor and manage your application.
spring-boot-starter-web : is used to write a Rest Endpoints
spring-boot-starter-actuator : monitor and manage your application.
spring-boot-starter-thymeleaf : to create web application
spring-boot-starter-test : monitor and manage your application.
spring-boot-starter-web : Spring and JPA for database access
spring-boot-starter-parent
Annotations

@SpringBootApplication : @EnableAutoConfiguration + @ComponentScan
@EnableAutoconfiguration : Automatically configures application based on the dependencies  
 @ComponentScan :scans all the beans and package declarations when the application initializes


Spring Initializer


 CommandLineRunner and ApplicationRunner, to run specific pieces of code when an application is fully started. These interfaces get called just before run() once SpringApplication completes.

When you want to execute some piece of code exactly before the application startup completes, you can use it then. In one of our projects, we used these to source data from other microservices via service discovery, which was registered in Consul.

Ordering

You can register as many application/command line runners as you want. You just need to register them as Beans in the application context. Then, Spring will automatically pick them up. You can order them as well either by extending interface org.springframework.core.Ordered or via the @Order annotation.



Both of them provides the same functionality and the only difference between CommandLineRunner and ApplicationRunner is CommandLineRunner.run() accepts String array[] whereas ApplicationRunner.run() accepts ApplicationArguments as argument. you can find more information with example here

_____________________________________________________________________________________
application.properties


Command Line Properties


@Value("${property_key_name}")

@Value("${spring.application.name:demoservice}")
SPRING RESTFUL DEPENDENCIES
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
           <artifactId>spring-boot-maven-plugin</artifactId>
@RestController : It serves JSON, XML and custom response.

@RequestMapping : Annotation is used to define the Request URI to access the REST Endpoints. 

@RequestBody annotation is used to define the request body content type.

ResponseEntity<Object>
@PathVariable annotation is used to define the custom or dynamic request URI. 

 ResponseEntity<Object> updateProduct(@PathVariable("id") String id)

 @RequestParam annotation is used to read the request parameters from the Request URL.

GET API 

 return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);
@RequestParam(value = "name", required = false, defaultValue = "honey") String name)
POST API
new ResponseEntity<>(productRepo.values(), HttpStatus.OK);

PUT API 
  return new ResponseEntity<>("Product is updated successsfully", HttpStatus.OK);
DELETE API
return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);
@ControllerAdvice 
The @ControllerAdvice is an annotation, to handle the exceptions globally

@ExceptionHandler

package com.tutorialspoint.demo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ProductExceptionController {
   @ExceptionHandler(value = ProductNotfoundException.class)
   public ResponseEntity<Object> exception(ProductNotfoundException exception) {
      return new ResponseEntity<>("Product not found", HttpStatus.NOT_FOUND);
   }
}




ResponseEntity.ok("Hello World!");
new ResponseEntity<>(
      "Custom header set", headers, HttpStatus.OK)


  return ResponseEntity.ok()
        .header("Custom-Header", "foo")
        .body("Custom header set");



2
3
4
5
6
@PostMapping(value = "/content", produces = MediaType.APPLICATION_JSON_VALUE)

@PostMapping(value = "/content", produces = MediaType.APPLICATION_XML_VALUE)


RestTemplate





For monitoring and managing your microservice application via Spring Boot Admin Server, you should add the Spring Boot Admin starter client dependency and point out the Admin Server URI into the application properties file.

Note − For monitoring an application, you should enable the Spring Boot Actuator Endpoints for your Microservice application.

First, add the following Spring Boot Admin starter client dependency and Spring Boot starter actuator dependency in your build configuration file.




Cross-Origin Resource Sharing (CORS) is a security concept that allows restricting the resources implemented in web browsers. It prevents the JavaScript code producing or consuming the requests against different origin.






Tuesday, January 23, 2018

Graph Algorithms:









Graph Algorithms:
Introduction, DFS and BFS:
  1. Graph and its representations
  2. Breadth First Traversal for a Graph
  3. Depth First Traversal for a Graph
  4. Applications of Depth First Search
  5. Detect Cycle in a Directed Graph
  1. Detect Cycle in a an Undirected Graph
  2. Detect cycle in an undirected graph
  3. Longest Path in a Directed Acyclic Graph
  4. Topological Sorting
  5. Check whether a given graph is Bipartite or not
  6. Snake and Ladder Problem
  7. Biconnected Components
  8. Check if a given graph is tree or not
Minimum Spanning Tree:
  1. Prim’s Minimum Spanning Tree (MST))
  2. Applications of Minimum Spanning Tree Problem
  3. Prim’s MST for Adjacency List Representation
  4. Kruskal’s Minimum Spanning Tree Algorithm
  5. Boruvka’s algorithm for Minimum Spanning Tree
Shortest Paths:
  1. Dijkstra’s shortest path algorithm
  2. Dijkstra’s Algorithm for Adjacency List Representation
  3. Bellman–Ford Algorithm
  4. Floyd Warshall Algorithm
  5. Johnson’s algorithm for All-pairs shortest paths
  6. Shortest Path in Directed Acyclic Graph
  7. Some interesting shortest path questions
  8. Shortest path with exactly k edges in a directed and weighted graph
Connectivity:
  1. Find if there is a path between two vertices in a directed graph
  2. Connectivity in a directed graph
  3. Articulation Points (or Cut Vertices) in a Graph
  4. Biconnected graph
  5. Bridges in a graph
  6. Eulerian path and circuit
  7. Fleury’s Algorithm for printing Eulerian Path or Circuit
  8. Strongly Connected Components
  9. Transitive closure of a graph
  10. Find the number of islands
  11. Count all possible walks from a source to a destination with exactly k edges
  12. Euler Circuit in a Directed Graph
  13. Biconnected Components
  14. Tarjan’s Algorithm to find Strongly Connected Components
Hard Problems:
  1. Graph Coloring (Introduction and Applications)
  2. Greedy Algorithm for Graph Coloring
  3. Travelling Salesman Problem (Naive and Dynamic Programming)
  4. Travelling Salesman Problem (Approximate using MST)
  5. Hamiltonian Cycle
  6. Vertex Cover Problem (Introduction and Approximate Algorithm)
  7. K Centers Problem (Greedy Approximate Algorithm)
Maximum Flow:
  1. Ford-Fulkerson Algorithm for Maximum Flow Problem
  2. Find maximum number of edge disjoint paths between two vertices
  3. Find minimum s-t cut in a flow network
  4. Maximum Bipartite Matching
  5. Channel Assignment Problem
Misc:
  1. Find if the strings can be chained to form a circle
  2. Given a sorted dictionary of an alien language, find order of characters
  3. Karger’s algorithm for Minimum Cut
  4. Karger’s algorithm for Minimum Cut | Set 2 (Analysis and Applications)
  5. Hopcroft–Karp Algorithm for Maximum Matching | Set 1 (Introduction)
  6. Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)
  7. Length of shortest chain to reach a target word
  8. Find same contacts in a list of contacts





Facebook.java
KthNonRepeatingString.java
MedianOfSortedArrays.java
PermutationsOfString.java
RegionMatchesString.java
RemoveCharacters.java
SmallestStringWindowProblem.java
SortByFrequency.java
SortUsingLastNames.java
SpecialString.java
String_comparsion_Example.java
String_replace_Example.java