Skip to content

Instantly share code, notes, and snippets.

View iamitshri's full-sized avatar
🦉
Focusing

iamitshri iamitshri

🦉
Focusing
View GitHub Profile
@iamitshri
iamitshri / annotationReflectionExample.java
Created October 9, 2018 21:02
Read Annotations on class using Reflection
// Get annotations on the class object
Annotation[] annotations = classobject.getClass()
.getAnnotations();
for (Annotation annotation : annotations) {
log.debug("annotation type: {}", annotation.annotationType());
Class<? extends Annotation> type = annotation.annotationType();
for (Method method : type.getDeclaredMethods()) {
Object val = method.invoke(annotation, (Object[]) null);
log.debug("method name: {}, return value:{} return type:{}", method.getName(), val, val.getClass()
.getName());
@iamitshri
iamitshri / RestResponseEntityExceptionHandler.java
Created October 9, 2018 21:43
Spring Boot Exception Handler that parses Response status annotation on the Exception class
package edu.wgu.dm.config;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@iamitshri
iamitshri / AsyncTaskExecutorSpringBoot.java
Created October 11, 2018 02:58
Execute tasks in spring
package edu.wgu.dm.datagenerator;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@iamitshri
iamitshri / JPA mysql springBoot.properties
Last active July 7, 2019 19:44
Spring Boot JPA configuration
spring.datasource.url=jdbc:mysql://host:3306/dbname?autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none
@iamitshri
iamitshri / notes-jpa.md
Last active November 11, 2018 01:35
JPA Notes

Efficient JPA Querying

MultipleBagFetchException

  • Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [com.jpa.experiments.entity.TaskEntity.aspects, com.jpa.experiments.entity.AspectEntity.anchors]
@iamitshri
iamitshri / configDemo.java
Last active November 18, 2018 21:47
Spring boot feature :- Configuration Properties demo
@Component
@Data
@ConfigurationProperties(prefix = "config.demo")
@Validated
public class ConfigPropertiesDemo {
int numTests;
@Size(min=3)
String name;
float expense;
boolean booleanYes;
@iamitshri
iamitshri / logback.xml
Created November 25, 2018 18:24
Logback configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="logPattern"
value="%d %-5level %logger{35} - %msg%n" />
<property name="logEncoding" value="UTF-8" />
<property name="logDirectory" value="logs" />
@iamitshri
iamitshri / get-File.java
Last active November 26, 2018 17:55
Get Files under the folder in src/main/resources in spring boot app
private void getFile() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
List<Resource> files = Arrays.asList(resolver.getResources("classpath*:folder-name"));
for(Resource file : files) {
for(File file1 : file.getFile().listFiles()) {
}
}
@iamitshri
iamitshri / Stopwatch.java
Created December 10, 2018 02:44
Using stopwatch from package org.apache.commons.lang3.time
package com.amit;
import org.apache.commons.lang3.time.StopWatch;
import lombok.extern.slf4j.Slf4j;
/**
* Hello world!
*
*/
@iamitshri
iamitshri / spring-boot-project.html
Last active January 12, 2019 03:05
One click Spring boot project
<h1>
This link helps to quickly create a sample spring boot project
</h1>
https://start.spring.io/starter.zip?name=autoTestExperiment&groupId=edu.wgu.ema&artifactId=autoTestExperiment&version=0.0.1-SNAPSHOT&description=Demo+project+for+Spring+Boot&packageName=edu.wgu.ema.katas&type=maven-project&packaging=jar&javaVersion=1.8&language=java&bootVersion=1.5.19.RELEASE&dependencies=lombok&dependencies=configuration-processor&dependencies=devtools&dependencies=mysql&dependencies=data-jpa&dependencies=web
------- ----------