라벨이 자바스크립트인 게시물 표시

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...

자바스크립트 숫자 확인

자바스크립트에서 문자열 또는 변수가 숫자인지 아닌지 확인 방법입니다.  NaN은 Not a Number 의 약자입니다. isNaN은 Number에 포함된 함수 입니다. console . log ( isNaN ( 0 )) // false console . log ( isNaN ( '0' )) // false console . log ( isNaN ( '' )) // false console . log ( isNaN ( 1 )) // false console . log ( isNaN ( '1' )) // false console . log ( isNaN ( 'a' )) // true 논리 부정연산자 ! 를 붙이면 숫자인지 여부를 반환합니다. console . log (! isNaN ( 0 )) // true console . log (! isNaN ( '0' )) // true console . log (! isNaN ( '' )) // true console . log (! isNaN ( 1 )) // true console . log (! isNaN ( '1' )) // true console . log (! isNaN ( 'a' )) // false Number 함수를 이용하여 확인해 볼게요. console . log ( Number ( 0 )) // 0 console . log ( Number ( '0' )) // 0 console . log ( Number ( '' )) // 0 console . log ( Number ( 1 )) // 1 console . log ( Number ( '1' )) // 1 console . log ( Number ( 'a' )) // NaN console . log ( Number ( 0 ) >= 0 ) // true co...

자바 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...

자바스크립트 Array 를 map 으로 변환, 일부만 변경, 합치기

javascript array to map Array(배열, 리스트, List)를 map으로 변환하여 특정키의 값이 존재하는지 여부를 찾고 싶을 때가 있습니다. (저는 자바를 주로 사용하기 때문에 자바 stream의 collect  사용하고 싶었습니다.) Array to map, list to map const books = [ { id : 'a' , name : '1' , author : 'q' } , { id : 'b' , name : '2' , author : 'w' } , { id : 'c' , name : '3' , author : 'e' } ] ; // array to map , list to map const bookMap = books. reduce ((acc , currentValue) => { acc[currentValue. id ] = currentValue ; return acc ; } , {}) ; console . log (bookMap) ; const newBooks = [ { id : 'b' , name : 'n2' , author : 'w' } , { id : 'c' , name : 'n3' , author : 'e' } , { id : 'd' , name : 'n4' , author : 'r' } , { id : 'f' , name : 'n5' , author : 't' } ] // 이전에 등록된 데이터 확인하기 const oldBooks = newBooks. filter (each => !bookMap[each. id ]) console . log (oldBooks) acc의 이전 결과 내...

자바 스프링 MVC에서 LocalDateTime 타입으로 파라미터 받기

웹에서 전달한 파라미터를 서버에서 문자열(String) 타입이 아닌 자바 시간 타입을 받으면 서버에서 별도의 문자열 포맷딩을 할 필요없이 사용할 수 있습니다. 웹 클라이언트에서 전달하는 코드 const startDate = moment ( '2021-01-13' , 'YYYY-MM-DD' ). startOf ( 'day' ). format ( 'YYYY-MM-DDTHH:mm:sszz' ) console . log (startDate) // //2021-01-13T00:00:00 axios. get ( "http://localhost/search?startDate" + startDate). then ((response) => { console . log (response) }) 위의 예시는 날짜기준 조회 예시입니다. 날짜 단위로 조회하기 위해 startOf 함수를 사용하여 시간을 0으로 변경하였다. 자바 스프링 MVC import org.springframework.format.annotation. DateTimeFormat ; // 클래스정의 생략 @GetMapping ( "/search" ) public void getSearch ( @DateTimeFormat (iso = DateTimeFormat .ISO. DATE_TIME ) @RequestParam LocalDateTime startDate) { // action } LocalDateTime의 타입으로 데이터를 받으려면, DateTiemFormat을 정의해주면 됩니다.

Moment.js 자주사용하는 함수

자바스크립트에서 Date 객체를 사용하여 날짜 관련 조작하기 까다로운일입니다. Moment 라이브러리를 활용하면 개발을 쉽게 할 수 있습니다. Moment 객체 -> Date 객체로 변환 const oDate = moment (). toDate () Date 객체 -> Moment 객체로 변환 const oMoment = moment ( new Date ( 2019 , 9 , 31 )) Moment 날짜 더하고, 빼기 moment (). add ( 1 , 'days' ) //내일 moment (). subtract ( 1 , 'days' ) //어제 단위 : seconds, minutes, hours, days, months, years 문자열 -> Moment 객체로 변환 moment ( "20191031" , "YYYYMMDD" ) Moment 객체 -> 문자열로 변환 moment (). format ( 'YYYYMMDD' ) 공식 사이트 https://momentjs.com/