Posts

Spring Form Validation

Image
 The Need for validation is to check the user input form for  require fields Validate numbers in a range validate format (Postal code) custom business rule Java standard bean Validation API Java has a standerd Bean validaton API Defines  metadata model and API for entity validation  Not tied to either the web tier or the persistence tier Available for server-side apps and also client-side javaFx/Swigns apps. Spring version 4 and higher supports Bean Validation API The preferred method for validation when building the spring app. that means only adding annotations provided by validation API. Simply add validation JAR to our Project. Bean Validation Features: Required - to check filed is required  validate length - to check the length of the input Validate numbers -  Validate with regular expression custom validation Validation Annotation: @NotNull - Checks that the annotation value is not null. @Min - Must be a number >= value @Max - Must be a number <...

Java Collection - Array

  if we want to create 1000 variables with the same datatype we need to declare 1000  new variables and it's very difficult for programmers to deal with such types of variables. to overcome this problem Collection framework is introduced. The collection is a set of interfaces and classes to represent data structure and Algorithm.  Array :  the Array store data by indexing. the index will start from 0 Array in an indexed-based collection that contains a fixed number of homogenous data elements. We can declare an Array Student[] s = new Student[1000]; Limitation:  The array is a fixed size so once we create an Array we can not change the size of the Array. The array can hold only homogeneous data type which means Array can only contain the same data type values. the Arrays are a fixed-size index-based collection due to there being chances of memory waste. for example, if we create an Array with 1000 sizes and in programming, we only add 100 values in the array the...

Microservices design patterns

Microservices are an architectural pattern where a large application is divided into smaller, independent services that communicate with each other through APIs. Here are some common patterns used in microservices architecture API Gateway Pattern: A centralized entry point for all client requests that routes requests to the appropriate service. Service Registry Pattern: A centralized service that acts as a directory of all available services, making it easy for services to discover each other and communicate. Circuit Breaker Pattern: A mechanism that prevents a service from making requests to a failed service, improving overall system stability. Load Balancer Pattern: A component that distributes incoming requests evenly across multiple instances of a service, improving reliability and scalability. Event-Driven Pattern: A communication pattern where services communicate with each other by sending and receiving events, rather than direct request-response calls. Bulkhead Pattern: A mecha...

Controller class in spring

 To initialize the controller in the spring we need the below annotations   @Controller : this is a specialization of the @Componet annotation.  the controller annotation indicates that the class is a controller type to spring while component scanning. by default, @Controller handler methods only return the view.  if we want to return JSON or XML response then need to add @ResponseBody annotation on every method in the class. @Controller public class HelloWordController { @RequestMapping( "/showform" ) public String showForm() { return "helloword-form" ; } @RequestMapping( "/showform2" ) @RequestBody public User showForm() { return new User("Nilesh","Kadam") ; } } @RestController : @RestController is a specialized version of @Controller annotation. this is a combination of @Controller and @ResponseBody annotation. RestController is useful for RESTfull web service because in RESTfull web service we on...

How to configure spring MVC using xml configuration

Image
The web browser sends the request to the front controller. the front controller is known as a dispatcher servlet. dispatcher servlet will redirect the request to the appropriate controller. the Controller will perform some operation to add or get the data into the model to fulfill requirements and return the view to display on the web browser. Spring MVC configuration steps Create web.xml and servlet-context.xml file in WebContent/WEB-INF folder. the Web.xml file contains the dispatcher servlet, and URL mapping configuration. the servlet-context.xml file containing component scanning, annotation-driven support, and view resolver configuration. Configure Spring MVC dispatcher servlet. (web.xml) <servlet> <servlet-name> dispatcher </servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-I...

Future interface in java

Future interface introduces in java 1.5 in java. util.concurrent package. It is used to hold the result of an asynchronous computation. The asynchronous result will automatically appear in the Future after the asynchronous process is completed. Mostly Future will use with Callable interface and ExecutorService to capture the asynchronous result.  Implementation of Future with thread public class FutureWithThread { private ExecutorService executor = Executors. newFixedThreadPool ( 20 ) ; public Future<Integer> makeDouble (Integer num){ return executor .submit(()->{ Thread. sleep ( 100 ) ; return num * num ; }) ; } } The interface provides the following four methods: get(): the method waits for the computation to complete the async operation and waits to retrieve its result. cancel() : suppose in case we want to stop the execution and we can use the future.cancel(true) to tell the executor service to stop the cur...

Microservice Architecture Advantages and Disadvantages

Microservices architecture is a method of developing software systems as a collection of small, independent services that communicate with each other over a network. Each microservice is responsible for a specific business function and runs its own process, which can be deployed and scaled independently of the other services. This approach to software development has several benefits: It allows for faster development and deployment of new features, as each microservice can be developed, tested and deployed independently. It allows for more flexible scaling, as different microservices can be scaled up or down depending on their usage. It promotes a culture of autonomy and ownership among development teams, as each team is responsible for the development and maintenance of their own microservice. It allows for the use of different technologies and programming languages for different microservices. It helps to reduce the complexity of the overall system, as each microservice can be unders...