Burninghering's Blog
article thumbnail
Server to server - GET
Spring 2022. 6. 26. 05:58

Client 서버 만들기 server.port=8080 package com.example.client.controller; import com.example.client.service.RestTemplateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestM..

Stream 활용 예제
JAVA 2022. 6. 22. 02:55

스트림을 활용하여 패키지 여행 비용 계산하기 문제 정의 여행사에 패키지 여행 상품이 있습니다. 여행 비용은 15세 이상은 100만원, 그 미만은 50만원 입니다. 고객 세 명이 패키지 여행을 떠난다고 했을 때 비용 계산과 고객 명단 검색등에 대한 연산을 스트림을 활용하여 구현해 봅니다. 고객에 대한 클래스를 만들고 ArrayList로 고객을 관리 합니다. 고객 정보는 다음과 같습니다. CustomerLee 이름 : 이순신 나이 : 40 비용 : 100 CustomerKim 이름 : 김유신 나이 : 20 비용 : 100 CustomerHong 이름 : 홍길동 나이 :13 비용 : 50 고객 클래스 public class TravelCustomer { private String name; //이름 priva..

reduce()
JAVA 2022. 6. 21. 15:12

reduce() 연산 Stream에서 정의된 연산이 아닌 프로그래머가 직접 구현한 연산을 적용 T reduce(T identify, BinaryOperator accumulator) //reduce 메소드의 두 번째 파라미터로 직접 구현 가능 //직접 구현하기 어렵다면 BinaryOperator를 상속받자 최종 연산으로 스트림의 요소를 소모하며 연산을 수행 배열의 모든 요소의 합을 구하는 reduce() 연산 구현 예 Arrays.stream(arr).reduce(0, (a,b)->a+b)); //초기값, 매개 변수, 매개 변수로 수행할 식 reduce() 메서드의 두 번째 요소로 전달되는 람다식에 따라 다양한 기능을 수행 할 수 있음 람다식을 직접 구현하거나 람다식이 긴 경우 BinaryOperator..

Stream
JAVA 2022. 6. 21. 14:37

스트림이란? 자료 처리에 대한 추상화, 일관적인 기능을 제공함 자료의 대상과 관계없이 동일한 연산을 수행일관성 있는 연산으로 자료의 처리를 쉽고 간단하게 함 자료 처리에 대한 추상화가 구현되었다고 함 배열, 컬렉션을 대상으로 동일한 연산을 수행 함 한번 생성하고 사용한 스트림은 재사용 할 수 없음, 다른 연산을 수행하기 위해서는 스트림을 다시 생성해야 함 자료에 대한 스트림을 생성하여 연산을 수행하면 스트림은 소모됨 스트림 연산은 기존 자료를 변경하지 않음 자료에 대한 스트림을 생성하면 스트림이 사용하는 메모리 공간은 별도로 생성되므로 연산이 수행되도 기존 자료에 대한 변경은 발생하지 않음 스트림 연산은 중간 연산과 최종 연산으로 구분 됨 최종연산이 호출되어야 중간 연산에 대한 수행이 이루어 지고 그 결..

article thumbnail
Spring Boot - Interceptor
Spring 2022. 6. 20. 18:16

Filter는 Web application에 등록된다 package com.example.intercepter.intercepter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import javax.servlet.http.HttpServletRequest; import..

article thumbnail
Spring boot - Filter
Spring 2022. 6. 18. 05:31

처음으로 Lombok 사용해보기! @Data를 쓰면, Getter/Setter뿐만 아니라 equals/hashCode/toString까지 만들어준다 Lombok이 프로그램이 실행될때는 필요없다 컴파일됐을 때 이미 클래스파일에다 생성자/Getter/Setter 등이 만들어져있기 때문에 여태까지는 sout를 사용하여 시스템 로그를 남겨왔는데, @Slf4j 어노테이션을 사용하면 로그를 쉽게 남길 수 있다. package com.example.filter.controller; import com.example.filter.dto.User; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; imp..

article thumbnail
Spring - DTO를 사용하는 이유
Spring 2022. 6. 16. 20:17

DTO란 무엇인가, VO와의 비교 오늘은 다음의 고민 때문에 글을 작성하게 되었다. DTO가 정확히 뭘 의미하는 거지? DTO를 꼭 써야하는 이유가 뭐지? DTO랑 VO를 많이 비교하던데, 뭐가 다른거지? DTO란 무엇인가 DTO(Data Transfer Object, kafcamus.tistory.com 위 블로그에서 자세하게 서술해주고 있다! VO와 DTO의 차이점에 대해서도 서술하고 있으니 참고하길.., 결론적으로 한번의 호출에 많은 데이터를 묶어 보내는 것이 효율적이기 때문이다. 책의 가격/저자/출판사 데이터를 다 따로 만들어 비즈니스 로직을 처리한다면 얼마나 비효율적이겠는가? 그러므로 요청에 대한 모든 데이터를 보관할 수 있는 데이터 전송 객체, DTO를 만들어 사용한다. Spring DTO의 ..

article thumbnail
Validation 모범 사례
Spring 2022. 6. 13. 23:01

Global하게 잡아주던 예외처리를 한 클래스에만 한정되어 지정해주게 함 package com.example.exception.advice; import com.example.exception.controller.ApiController; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.an..