본문 바로가기

Spring & Java

Java conver t JsonArray to List public static List jsonArrayToObjectList(String json, Class target) throws IOException { ObjectMapper mapper = new ObjectMapper(); CollectionType listType = mapper.getTypeFactory() .constructCollectionType(ArrayList.class, target); List ts = mapper.readValue(json, listType); log.debug("TargetClass: {}", ts.get(0).getClass().getName()); return ts; } //TargetClass class Target {} //sample json arr.. 더보기
LocalDateTime hour step //1. step 1 hour var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime start = LocalDateTime.parse("2021-10-30 00:00:00", formatter), end = LocalDateTime.parse("2021-10-31 00:00:00",formatter); Stream.iterate(start, dt->dt.plusHours(1)) .limit(ChronoUnit.HOURS.between(start, end) + 1) .forEach(System.out::println); //2. step 1 hour var formatter = DateTimeFormatter.of.. 더보기
ModelMapper 를 사용하여 객체 컨버팅 하기 서로 다른 클래스의 값을 복사 해주는 라이브러리이며, 필드명 일치 혹은 유사 필드 , 값일 없는 필드 복사 스킵 등 을 쉽게 도와 주는 라이브러리이다. 사용을 위한 dependency org.modelmapper modelmapper 2.3.8 ​ 사용 방법 //A 클래스를 B 클래스로 컨버팅 예제 ModelMapper modelMapper = new ModelMapper(); //MatchingStrategies 는 여러가지가 존재하며 상황에 맞게 사용 하도록 한다. //여기서는 필드명 완전일치 할 경우에만 진행 하도록 한다. modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); B b = modelMapper.ma.. 더보기
Spring AOP 관점 지향 프로그램 예: 보안 관련된 관심사, API 성능로깅, 트랜잭션 관리,요청 로깅 등 Aspect: 관심사를 모듈화 한것이다. 여러 클래스에 걸쳐 코드를 분산하는 대신 관심사를 다루는 로직을 하나의 Aspect 에 넣는다. 예: isValidatedMember() 와 같이 검증 메서드를 여러 클래스에 초기 진입부에서 검증 스프링에서 클래스에 Aspect 를 구현 할 수 있으며, @Aspect 어노테이션을 적용 할수있다. dependency: org.springframework.boot:spring-boot-starter-aop AOP 적용 전: classDemoA -> methodDemoA( isValidatedMemebr() ) classDemoB -> methodDemoB( isValidate.. 더보기
spring factory pattern Factory 패턴: 하위 클래스가 어떤 객체를 생성할지를 결정 하도록 하는 패턴. 스프링에서 빈을 컬렉션으로 주입할 수 있다. //타입 public enum AuthenticationType { PASSWORD, DEVICE, FIDO } //인터페이스 public interface AuthenticationService { AuthenticationType getAuthentiicationType(); void validateAuthentication(); } //비밀번호 인증구현체 @Slf4j @Service public class AuthenticationPasswordService implements AuthenticationService{ @Override public Authenticatio.. 더보기
spring webflux swagger config - spring boot version org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE - swagger maven dependency io.springfox springfox-boot-starter 3.0.0 io.springfox springfox-oas 3.0.0 - swagger config @Configuration @EnableOpenApi public class SwaggerConfig { @Bean public Docket apiConfig() { return new Docket(DocumentationType.SWAGGER_2) .groupName("version_1.0") .enable(true) .select() ... 더보기
React Stream create and test //배열로 부터 리액트 타입 생성 public Flux creatFluxFromArray(){ String[] countryArray = new String[] { "Korea","America","China","Canada" }; Flux countryFlux = Flux.fromArray(countryArray); return countryFlux; } // 컬렉션 타입으로부터 리액트 타입 생성 public Flux creatFluxFromIterable(){ List countryList = Arrays.asList("Korea","America","China","Canada"); Flux countryFlux = Flux.fromIterable(countryList); return countryF.. 더보기
Spring boot kafka demo with docker-compose spring boot kafka demo with docker ref kafka with springboot description kafka ref kafka 는 pub-sub 모델을 기반으로 동작한다. 구성요소: zookeeper: producer 와 consumer 를 관리한다. producer: topic 의 메시지 생성후 해당 메시지를 broker 에 전달 consumer: topic 을 subscribe 한다. 메시지를 pull 방식으로 broker 로부터 가져오기 때문에 batch consumer 구현 가능. broker: topic 을 기준으로 메시지 관리 전달받은 메시지를 topic 별로 분류하여 적재 topic: partition 단위로 구성 된다. 클러스터 서버에 분산 저장할 경우 pa.. 더보기