Skip to content

Instantly share code, notes, and snippets.

View PRASANTHRAJENDRAN's full-sized avatar
💭
Always engulfed in coding

PRASANTH RAJENDRAN PRASANTHRAJENDRAN

💭
Always engulfed in coding
View GitHub Profile
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / DeepCloneExample.java
Created June 24, 2024 06:56
Perform Deep copy properties from one Java bean to another
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import java.util.Collections;
import java.util.List;
public class DeepCloneExample {
public static void main(String[] args) throws Exception {
SourceBean source = new SourceBean();
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / JaxRSRequestResponseLoggerFilter .java
Last active June 20, 2024 06:17
JaxRS Request and Response logging filter code sample
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Priority;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
import software.amazon.awssdk.services.sns.model.PublishRequest;
import software.amazon.awssdk.services.sns.model.PublishResponse;
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / AppSecurityConfig.java
Last active September 30, 2022 09:08
Cors issue after upgrading to use Spring Boot 2.4.0 -> java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*"since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOrigin…
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / PhotoServiceITest.java
Created April 14, 2020 11:06
Test case to validate the crud operations with caching
import com.demo.spring.data.redis.cache.demo.SpringBootStarterDataRedisDemoApplication;
import com.demo.spring.data.redis.cache.demo.domain.Photo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / RedisCacheConfigurationITest.java
Created April 14, 2020 11:01
Test case to validate the configured TTL properties that is set to RedisCacheManager or not
package com.demo.spring.data.redis.cache.demo.configuration;
import com.demo.spring.data.redis.cache.demo.SpringBootStarterDataRedisDemoApplication;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.test.context.ActiveProfiles;
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / PhotoService.java
Created April 14, 2020 10:52
Sample PhotoService with CRUD methods that uses spring boot caching
@Service
public class PhotoService {
@CachePut(cacheNames = "photo", key = "#photo.id")
public Photo createPhoto(Photo photo) {
return photo;
}
@Cacheable(cacheNames = "photo", key = "#id")
public Photo findById(String id) {
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / RedisCacheConfiguration.java
Created April 14, 2020 10:39
RedisCacheManager configuration with Lettuce
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / pom.xml
Created April 14, 2020 10:35
Dependencies required for Redis Cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
@PRASANTHRAJENDRAN
PRASANTHRAJENDRAN / application.yml
Created April 14, 2020 10:13
Configuration required for redis cache
cache:
host: localhost
port: 6379
default-ttl: 6000
caches-ttl:
photo: 3600