REST API의 응답 JSON에 데이터가 없을 때의 표현

6335 단어 JSONresttech
REST API에서 대상 항목에 데이터가 없을 때의 표현 방법을 몰라서 정리해봤습니다.
큰 것은 두 개의 파벌이 있다.
  • null
  • undefined
  • 정의되지 않은 데이터 항목
  • null파


    RFC 8259에서는 JSON이 정의됩니다.
    JSON의 값은 객체, 배열, 숫자, 문자열 또는 가짜, 비어 있음, 진짜를 나타냅니다.
    따라서 데이터가 존재하지 않으면null을 지정해야 합니다.

    인용하다


    RFC 8259 참조
    3.  Values
    
       A JSON value MUST be an object, array, number, or string, or one of
       the following three literal names:
    
          false
          null
          true
    
       The literal names MUST be lowercase.  No other literal names are
       allowed.
    
          value = false / null / true / object / array / number / string
    
          false = %x66.61.6c.73.65   ; false
    
          null  = %x6e.75.6c.6c      ; null
    
          true  = %x74.72.75.65      ; true
    
    5.  Arrays
    
       An array structure is represented as square brackets surrounding zero
       or more values (or elements).  Elements are separated by commas.
    
       array = begin-array [ value *( value-separator value ) ] end-array
    
       There is no requirement that the values in an array be of the same
       type.
    

    인용원


    https://www.rfc-editor.org/rfc/rfc8259

    기타(Java)


    Java의 Spring 참조 버전에 따라 데이터가 없는 경우 null이 자주 표시됩니다.
    버전: 2.11은 "AllWAYS"입니다.
    https://github.com/FasterXML/jackson-annotations/blob/2.11/src/main/java/com/fasterxml/jackson/annotation/JsonInclude.java#L68
    Jackson의 최신 버전은 USE DEFAULTS입니다.초기 값을 명시적으로 정의한 경우 이 값을 사용합니다.
    private String id = "";
    
    https://github.com/FasterXML/jackson-annotations/blob/master/src/main/java/com/fasterxml/jackson/annotation/JsonInclude.java#56
    Java의 Spring으로 이 기능을 변경하려면 다음 설정 파일을 명시적으로 설정합니다.
    
    spring.jackson.default-property-inclusion: ALLWAYS
    

    undefined 파벌


    Google의 JSON Style Guide에 따라 데이터가 없으면 항목을 제외해야 합니다.
    또한 OpenAPI는 데이터 형식으로 null을 정의하지 않았습니다.nulllable로 정의할 수 있기 때문에,null의 표현 자체는 가능하지만, 데이터 형식으로 정의되지 않은 것으로 읽을 수도 있습니다. = 항목을 제외해야 합니다.

    인용하다


    구글의 JSON Style Guide.
    Consider removing empty or null values.
    If a property is optional or has an empty or null value, consider dropping the property from the JSON, unless there's a strong semantic reason for its existence.
    
    Open API의 데이터 유형입니다.
    Data Types
    
    The data type of a schema is defined by the type keyword, for example, type: string. OpenAPI defines the following basic types:
    string (this includes dates and files)
    
    - number
    - integer
    - boolean
    - array
    - object
      
    These types exist in most programming languages, though they may go by different names. Using these types, you can describe any data structures.
    
    Note that there is no null type; instead, the nullable attribute is used as a modifier of the base type.
    

    인용원


    https://google.github.io/styleguide/jsoncstyleguide.xml#Empty/Null_Property_Values
    https://swagger.io/docs/specification/data-models/data-types/

    기타(Java)


    자바의 스프링을 undefined로 변경하려면 다음 설정 파일을 명확하게 설정하십시오.
    spring.jackson.default-property-inclusion: NON_EMPTY
    

    도대체 뭐가 좋아요?


    데이터 구조를 항상 표현하기 위해 JSON의 RFC에 따라null에서 제공하는 API가 많은 것 같습니다.
    하지만 JSON 자체를 데이터로 볼 때 없는 데이터의 표현도 어쩔 수 없기 때문에 undefined도 가능하다.헛된 통신 데이터를 늘려도 어쩔 수 없다.API로 두드릴 때 데이터에 따라 반납 구조가 다를 수 있어 사용하는 쪽으로부터도 어려울 수 있다는 우려가 나온다.
    그것에 근거하여 나는 이렇게 생각한다.
  • Open API에 정의된 API
  • undefined
  • 정의된 API가 Open API에 제공되지 않음
  • null
  • undefined를 원한다면, 조건은 응답하는 JSON을 명확하게 정의할 수 있습니다.Excel과 Markedown으로 API 규격서를 정의할 수 있지만 typo 등을 포함한 유형이 반드시 일치하는 것은 아니다.
    API가 Open API의 YAML 파일에서 생성된 경우에도 유형이 명확하지 않습니다.
    앞으로 Open API 이후에 API 표현에 편리한 프로그램 라이브러리가 생겼다면 거기서 표현해야 한다.

    끝맺다


    내용이 소박해 매번 조사하고 싶었지만 RFC 링크와 스프링의 기본값 등을 조사해 스스로 정리했다.
    이 기사가 작동한다면 앞으로도 다양한 SNS에 정보를 공유할 테니 잘 부탁드립니다.
  • 기술 블로그 여기 있습니다.
  • 여기는 잡기 블로그입니다.
  • 좋은 웹페이지 즐겨찾기