Posts

Spring Core - BeanPostProcessor - Example

Image
In Spring framework, the BeanPostProcessor is an interface that contains two callback methods: postProcessBeforeInitialization  and postProcessAfterInitialization .  BeanPostProcessor allows custom modification of new bean instances that are created by Spring Bean Factory.  If we want to implement some custom logic such as checking for marker interfaces or wrapping beans with proxies after the Spring container finishes instantiating, configuring, and initializing a bean by plugging in one or more BeanPostProcessor implementations. As we mentioned earlier, BeanPostProcessor consists of two callback methods: 1.  postProcessBeforeInitialization :  We can call this method to apply any custom logic (such as InitializingBean's afterPropertiesSet or a custom init-method) to the given new bean instance before any bean initialization callbacks. 2.  postProcessAfterInitialization :  We can call this method to apply any custom logic (such as InitializingBean's afterPropertiesSet or a cust

Spring Boot WireMock - Testing HTTP clients

Image
In this section, we'll develop a Spring Boot application that connects with a jsonplaceholder client with the help of RestClient and at the end  we will perform integration testing with WireMock  and   JUnit 5 . jsonplaceholder JSONPlaceholder is a free online REST API that we can use whenever we need some fake data. RestClient RestClient is a synchronous client to perform HTTP requests. It is a higher-order API since it performs HTTP requests by using an HTTP client library like the JDK HttpClient, Apache HttpComponents, and others. More Info - click here WireMock WireMock is a popular open-source library for API mock testing. More Info - click here Advantages :      > No load on the remote server.      > Full control over the response.      > Works offline.           JUnit 5 For writing and running unit tests and integration tests for any Java application, the JUnit framework is a great option. It is already included in the spring-boot-starter-test module of Spring Boo

Spring: HandlerInterceptor Example

Image
In this section, we will learn about using Spring HandlerInterceptor in Spring Boot application. The Spring framework includes HandlerInterceptors, which are positioned in between DispatcherServlet and our Controllers. The primary purpose of interceptor is to intercept incoming request and response. It perform some operations on request and response. In Spring, an interceptor can be implemented by implementing HandlerInterceptor Interface. There are numerous uses for interceptors, some of which are mentioned below: 1. Logging Monitoring and troubleshooting running apps require logging. Using a combination of interceptors and Spring Boot's logging framework, we can log all requests, responses, and errors in a single location within an application. 2. Authentication & Authorization Incoming request can be intercepted by the Interceptor & authentication or authorization can be done, if denied then the request does not get delegated to the controller. 3. Request Response Modifi