본문 바로가기

spring

(8)
Spring Webflux ExceptionHandler Functional Endpoints Webflux에서 Annotated Controllers 패턴에서는 주로 MVC와 비슷하게 @ControllerAdvice 어노테이션과 @ExceptionHandler 어노테이션을 이용하는데요. Functional Endpoints 패턴에서는 주로 ErrorWebExceptionHandler을 활용합니다. 해당 인터페이스를 구현한 AbstractErrorWebExceptionHandler를 사용해볼거에요. AbstractErrorWebExceptionHandler 해당 생성자가 Deprecated 되었습니다. (Deprecated since 2.4.0 for removal in 2.6.0 in favor) @Deprecated public AbstractErrorWebExceptionHandler(Err..
Spring Boot ReactiveRedisTemplate<String, Integer> ReactiveRedisTemplate에서 value의 타입을 정하는 설정 코드에요. Java @Configuration public class RedisConfiguration { @Bean public ReactiveRedisTemplate numberRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { StringRedisSerializer keySerializer = new StringRedisSerializer(); GenericToStringSerializer valueSerializer = new GenericToStringSerializer(Integer.class); RedisSerializationContext.Redis..
Spring Webflux Annotated Controller @ClientIp 먼저 Spring Webflux Annotated Controller 방식에서 client ip를 가져와볼게요. 그 다음 MVC 방식에서는 어떻게 설정하면 되는지도 다뤄볼게요. @ClientIp 어노테이션을 만들어서 비즈니스 로직 단에서는 매우 간단하게 처리하고 IP를 받아오는 로직은 Resolver에 존재하도록 하겠습니다. 일단 목표는 Controller에서 아래와 같이 사용하는 거에요. Controller @RestController @RequiredArgsConstructor @Slf4j public class BookController { private final BookService bookService; @GetMapping("/books/{bookId}") public Mono getBook..
Spring Boot Reactive Mongo Data QueryDSL Spring Data MongoDB에서도 QueryDSL을 사용할 수 있어요. build.gradle에 아래 설정만 잘 추가해주시면 됩니다. 예제에서는 Reactive Mongodb를 사용하고 있습니다. build.gradle plugins { id 'com.ewerk.gradle.plugins.querydsl' version '1.0.10' } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive' implementation 'com.querydsl:querydsl-mongodb' implementation 'com.querydsl:querydsl-apt' } def querydsl..
No identifier specified for entity (JPA) 스프링 부트(Spring Boot) 애플리케이션을 실행했는데 오류가 났어요. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.exa..
자바 원격서버 파일 다운로드 (Java, Spring) 원격 서버에 위와 같은 이미지가 있다고 가정해볼게요. 자바에서 원격 서버의 파일을 다운로드 받아서 저장하는 코드를 작성해보겠습니다. 자바 버전은 1.8 이상으로 사용해야 코드 사용 가능합니다. Java 별도 라이브러리를 사용하지 않았어요. import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; // 원격 파일 다운로드 URL String fileUrl = "https://k.kakaocdn.net/dn/crbxqN/btqAPdDwLQJ/..
Spring Boot 파일 다운로드 (Controller) 스프링부트에서 컨트롤러를 만들어서 이미지를 다운로드하는 코드에요. 부트 환경이 아니여도 스프링 프레임워크를 사용하고 있다면 어디에나 적용 가능합니다. 코드를 간단하게 설명드리자면 파일 데이터에서 직접 Content-Type을 조사하여 Response Header에 세팅하고 InputStream으로 Resource를 생성하고 body에 세팅하여 전송합니다. Content-Disposition 값을 inline으로 하거나 헤더를 설정하지 않으면 웹 브라우저 내에서 이미지 확인이 가능합니다. (default가 inline) @GetMapping("download") public ResponseEntity download() throws IOException { Path path = Paths.get("C:/U..
자바 파일 복사 (+ Spring) Java에서 파일을 복사하는 코드에요. Java 기존 java.io File in = new File("C:\\Temp\\abc.txt"); File out = new File("C:\\Temp\\def.txt"); try (InputStream fis = new FileInputStream(in); OutputStream fos = new FileOutputStream(out)) { int bytesRead = 0; while ((bytesRead = fis.read()) != -1) { fos.write(bytesRead); } fos.flush(); } catch (Exception e) { e.printStackTrace(); } java.nio Path in = Paths.get("C:\\Tem..