6월, 2022의 게시물 표시

Spring Boot Admin Server, Client config 설정하기

이미지
 Spring Boot로 많은 프로젝트를 진행하게 됩니다. 많은 모니터링 도구가 있지만, Spring Boot 어플리케이션을 쉽게 모니터링 할 수 있는 방법을 소개하려고 합니다.   코드 중심으로 살펴보겠습니다. 1. 어드민 서버 구축 1-1. 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server implementation 'de.codecentric:spring-boot-admin-starter-server:2.5.4' } 1-2. 어드민 서버 설정 활성화 @SpringBootApplication @EnableAdminServer public class ServerApplication { public static void main (String[] args) { SpringApplication. run (ServerApplication. class, args) ; } } EnableAdminServer를 하면 됩니다. 2. 클라이언트 서버 설정  예제는 book-client, member-client 2가지 클라이언트, member-client가 2개의 인스턴스 실행으로 작성했습니다.  2-1 디펜던시 추가 dependencies { // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4' // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator implementation 'org.spring...

자바 URL 인코딩 디코딩

이미지
URL을 변수로 할당하여 개발할 때가 많이 있습니다. 예를 들어 소셜 로그인 후에 redirect_uri를 query string으로 전달 되는 것을 보실 수 있습니다. 이때 query string에  URL이 전달 되는경우에 인코딩을 해야 기대하는 결과를 받아 볼 수 있습니다. http://localhost?redirect_uri=https://www.youtube.com/watch?v=9PPaSkVIu98&list=all 위와 같이 전달하게 되면  redirect_uri  = https://www.youtube.com/watch?v=9PPaSkVIu98 list = all 원하지 않는 결과를 만나게 됩니다. 이외에 공백이라든지 특수문자를 사용하는 경우에도 인코딩해야합니다. 자바에서 인코딩, 디코딩 하는 방법을 살펴보겠습니다. @Test public void test () throws UnsupportedEncodingException { String url = "https://www.youtube.com/watch?v=9PPaSkVIu98" ; log .debug( "url === {}" , url) ; String encodedUrl = URLEncoder. encode (url , "UTF-8" ) ; log .debug( "encodedUrl === {}" , encodedUrl) ; String decodedUrl = URLDecoder. decode (encodedUrl , "UTF-8" ) ; log .debug( "decodedUrl === {}" , decodedUrl) ; } 실행 결과  url === https://www.youtube.com/watch?v=9PPaSkVIu98&list=all encodedUrl === https%3A%2F%2Fwww.youtube.com%2Fwatch...

자바 LocalDate json 변환에 관하여

이미지
 안녕하세요. 자바가 java.time 패키지를 제공하면서, 기존 Date 객체에 비하여 개발이 편해졌습니다. LocalDate를 json으로 변환하여 사용할때 만나는 문제를 다뤄보겠습니다. jackson 라이브러리를 사용하여 json으로 변환하고 다시 읽어 보겠습니다. jackson 라이브러리 json 변환 예제1 @Test public void objectMapper () throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper() ; LocalDate ld = LocalDate. now () ; String json = objectMapper.writeValueAsString(ld) ; System. out .println( "json == " + json) ; LocalDate ldFromJson = objectMapper.readValue(json , LocalDate. class ) ; System. out .println( "ld1 == " + ld) ; System. out .println( "ld2 == " + ldFromJson) ; } 실행 결과1 json으로 변환할때 LocalDate의 모든 속성이 그대로 변환된 것을 볼 수 있습니다. 이경우 serializer, deserializer를 구현하여 형식을 정할 수 있습니다. jackson 라이브러리 json 변환 예제2 import java.time.LocalDate ; import java.time.format.DateTimeFormatter ; import com.fasterxml.jackson.core.JsonProcessingException ; import com.fasterxml.jackson.databind.ObjectMapper ; import com.fasterxml.jackson.databind.mo...