Posts

Showing posts from July, 2020

Create cache with expire time

first use @EnableCaching annotation in your Main or configuration class import com.google.common.cache.CacheBuilder; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class CachingConfig {     @Bean     public CacheManager cacheManager() {         return new ConcurrentMapCacheManager() {             @Override             protected Cache createConcurrentMapCache(String name) {                 return new ConcurrentMapCache(                         name,                         CacheBuilder.newBuilder()                                 .expireAfterWrite(10, TimeUnit.SECONDS)                                 .build().asMap(),                         fal

git config - for a single repository

$ git config user.name "John Doe" $ git config user.email johndoe@example.com

send request with body - RestTemplate

HttpHeaders headers = new HttpHeaders();  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();  data.add("client_id",clientId);  data.add("client_secret",clientSecret);  data.add("grant_type",grantType);  data.add("redirect_uri",redirectUrl);  data.add("code",code.split("#")[0]);  HttpEntity<MultiValueMap<String, String>>  request = new HttpEntity<>(data, headers);  ResponseEntity<String> response = restTemplate.postForEntity(accessTokenUrl,request,String.class);

json to object - object to json methods

import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; public class JsonHelper {     public static String getJsonString(Object object) throws JsonProcessingException {         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);         return mapper.writeValueAsString(object);     }     public static <T> T getObjectFromJson(String json, Class<T> type) throws JsonProcessingException{         ObjectMapper mapper = new ObjectMapper();         mapper.registerModule(new JavaTimeModule());         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);         return mapper.readValue(json, type);     } }