SpringFramework/RestAPI

JSON표준 규약 에 의한 javascirpt -> spring 간의 REST(fetch, ajax, axios)통신에서 null과 undefined 부정 타입 변환

유혁스쿨 2023. 11. 30. 12:46
728x90
반응형

[예시 1. null]

  useEffect(()=>{
    let params = {params: {"test":null}}
    axios.get('/test', params)
    .then((response)=> {
      console.log(response)
    })
    .catch((error) => {
        console.log(error);
    })
  })
    @GetMapping("/test")
    public void test(String test) {
        System.out.println(test.equals("null"));
    }

 

[예시 2. undefined]

 

  useEffect(()=>{
    let params = {params: {"test":undefined}}
    axios.get('/test', params)
    .then((response)=> {
      console.log(response)
    })
    .catch((error) => {
        console.log(error);
    })
  })

 

    @GetMapping("/test")
    public void test(String test) {
        System.out.println(test == null);
    }

 

두 유형 모두 true로 출력된다.

 

null을 넘길 경우 문자열 "null"로 변환되어 받게되고,

undefined를 넘길 경우 null로 변환되어 받게된다.

 

JSON표준에 따른 규약에 의해서 위와 같은 현상이 발생한다.

 

+ 부록

null : 비어있음을 의미하는 데이터

undefined : 데이터 자체가 비어있는 상태를 의미

728x90
반응형