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.






Monday, January 22, 2018

Linkedlist :Questions




Linked List Data Structure

Topic :
Singly Linked List :
  1. Introduction to Linked List
  2. Linked List vs Array
  3. Linked List Insertion
  4. Linked List Deletion (Deleting a given key)
  5. Linked List Deletion (Deleting a key at given position)
  6. Find Length of a Linked List (Iterative and Recursive)
  7. Search an element in a Linked List (Iterative and Recursive)
  8. Swap nodes in a linked list without swapping data
  9. Write a function to get Nth node in a Linked List
  10. Print the middle of a given linked list
  11. Nth node from the end of a Linked List
  12. Write a function to delete a Linked List
  13. Write a function that counts the number of times a given int occurs in a Linked List
  14. Reverse a linked list
  15. Detect loop in a linked list
  16. Merge two sorted linked lists
  17. Generic Linked List in C
  18. Function to check if a singly linked list is palindrome
  19. Intersection point of two Linked Lists.
  20. Recursive function to print reverse of a Linked List
  21. Remove duplicates from a sorted linked list
  22. Remove duplicates from an unsorted linked list
  23. Pairwise swap elements of a given linked list
  24. Move last element to front of a given Linked List
  25. Intersection of two Sorted Linked Lists
  26. Delete alternate nodes of a Linked List
  27. Alternating split of a given Singly Linked List
  28. Identical Linked Lists
  29. Merge Sort for Linked Lists
  30. Reverse a Linked List in groups of given size
  31. Reverse alternate K nodes in a Singly Linked List
  32. Delete nodes which have a greater value on right side
  33. Segregate even and odd nodes in a Linked List
  34. Detect and Remove Loop in a Linked List
  35. Add two numbers represented by linked lists | Set 1
  36. Delete a given node in Linked List under given constraints
  37. Union and Intersection of two Linked Lists
  38. Find a triplet from three linked lists with sum equal to a given number
  39. Rotate a Linked List
  40. Flattening a Linked List
  41. Add two numbers represented by linked lists | Set 2
  42. Sort a linked list of 0s, 1s and 2s
  43. Flatten a multilevel linked list
  44. Delete N nodes after M nodes of a linked list
  45. QuickSort on Singly Linked List
  46. Merge a linked list into another linked list at alternate positions
  47. Pairwise swap elements of a given linked list by changing links
  48. Given a linked list of line segments, remove middle points
  49. Clone a linked list with next and random pointer | Set 1
  50. Clone a linked list with next and random pointer | Set 2
  51. Insertion Sort for Singly Linked List
  52. Point to next higher value node in a linked list with an arbitrary pointer
  53. Rearrange a given linked list in-place.
  54. Sort a linked list that is sorted alternating ascending and descending orders.
  55. Select a Random Node from a Singly Linked List
  56. Merge two sorted linked lists such that merged list is in reverse order
  57. Compare two strings represented as linked lists
  58. Rearrange a linked list such that all even and odd positioned nodes are together
  59. Rearrange a Linked List in Zig-Zag fashion
  60. Add 1 to a number represented as linked list
  61. Point arbit pointer to greatest value right side node in a linked list
  62. Merge two sorted linked lists such that merged list is in reverse order
  63. Check if a linked list of strings forms a palindrome
  64. Sort linked list which is already sorted on absolute values
  65. Delete last occurrence of an item from linked list
  66. Delete a Linked List node at a given position
  67. Linked List in java
  68. In-place Merge two linked lists without changing links of first list
  69. Delete middle of linked list
  70. Merge K sorted linked lists | Set 1
  71. Decimal Equivalent of Binary Linked List
  72. Flatten a multi-level linked list | Set 2 (Depth wise)
  73. Rearrange a given list such that it consists of alternating minimum maximum elements
  74. Subtract Two Numbers represented as Linked Lists
  75. Find pair for given sum in a sorted singly linked without extra space
  76. Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
  77. Partitioning a linked list around a given value and keeping the original order
  78. Check linked list with a loop is palindrome or not
  79. Clone a linked list with next and random pointer in O(1) space
  80. Length of longest palindrome list in a linked list using O(1) extra space
  81. Adding two polynomials using Linked List
  82. Implementing Iterator pattern of a single Linked List
  83. Move all occurrences of an element to end in a linked list
  84. Remove all occurrences of duplicates from a sorted Linked List
  85. Remove every k-th node of the linked list
  86. Check whether the length of given linked list is Even or Odd
  87. Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
  88. Multiply two numbers represented by Linked Lists
  89. Union and Intersection of two linked lists | Set-3 (Hashing)
  90. Find the sum of last n nodes of the given Linked List
  91. Count pairs from two linked lists whose sum is equal to a given value
  92. Merge k sorted linked lists | Set 2 (Using Min Heap)
  93. Recursive selection sort for singly linked list | Swapping node links
  94. Find length of loop in linked list
  95. Reverse a Linked List in groups of given size | Set 2
  96. Insert node into the middle of the linked list
  97. Merge two sorted lists (in-place)
  98. Sort a linked list of 0s, 1s and 2s by changing links
  99. Insert a node after the n-th node from the end
  100. Rotate Linked List block wise
  101. Count rotations in sorted and rotated linked list
  102. Make middle node head in a linked list
Circular Linked List:
  1. Circular Linked List Introduction and Applications,
  2. Circular Linked List Traversal
  3. Split a Circular Linked List into two halves
  4. Sorted insert for circular linked list
  5. Check if a linked list is Circular Linked List
  6. Convert a Binary Tree to a Circular Doubly Link List
  7. Circular Singly Linked List | Insertion
  8. Deletion from a Circular Linked List
  9. Circular Queue | Set 2 (Circular Linked List Implementation)
  10. Count nodes in Circular linked list
  11. Josephus Circle using circular linked list
  12. Convert singly linked list into circular linked list
Doubly Linked List:
  1. Doubly Linked List Introduction and Insertion
  2. Delete a node in a Doubly Linked List
  3. Reverse a Doubly Linked List
  4. The Great Tree-List Recursion Problem.
  5. Copy a linked list with next and arbit pointer
  6. QuickSort on Doubly Linked List
  7. Swap Kth node from beginning with Kth node from end in a Linked List
  8. Merge Sort for Doubly Linked List
  9. Create a Doubly Linked List from a Ternary Tree
  10. Find pairs with given sum in doubly linked list
  11. Insert value in sorted way in a sorted doubly linked list
  12. Delete a Doubly Linked List node at a given position
  13. Count triplets in a sorted doubly linked list whose sum is equal to a given value x
  14. Remove duplicates from a sorted doubly linked list
  15. Delete all occurrences of a given key in a doubly linked list
  16. Remove duplicates from an unsorted doubly linked list
  17. Sort the biotonic doubly linked list
  18. Sort a k sorted doubly linked list
  19. Convert a given Binary Tree to Doubly Linked List | Set
Misc :
  1. Skip List | Set 1 (Introduction)
  2. Skip List | Set 2 (Insertion)
  3. Skip List | Set 3 (Searching and Deletion)
  4. Reverse a stack without using extra space in O(n)
  5. An interesting method to print reverse of a linked list
  6. Linked List representation of Disjoint Set Data Structures
  7. Sublist Search (Search a linked list in another list)
  8. Doubly Circular Linked List | Set 1 (Introduction and Insertion)
  9. Doubly Circular Linked List | Set 2 (Deletion)
  10. How to insert elements in C++ STL List ?
  11. Unrolled Linked List | Set 1 (Introduction)
  12. A Programmer’s approach of looking at Array vs. Linked List
  13. How to write C functions that modify head pointer of a Linked List?
  14. Given a linked list which is sorted, how will you insert in sorted way
  15. Can we reverse a linked list in less than O(n)?
  16. Practice questions for Linked List and Recursion
  17. Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
  18. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
  19. Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
  20. Squareroot(n)-th node in a Linked List
  21. Find the fractional (or n/k – th) node in linked list
  22. Find modular node in a linked list
  23. Construct a linked list from 2D matrix
  24. Find smallest and largest elements in singly linked list
  25. Arrange consonants and vowels nodes in a linked list
  26. Partitioning a linked list around a given value and If we don’t care about making the elements of the list “stable”
  27. Modify contents of Linked List
Quick Links :
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above