π μλ‘
μλ νμΈμ! μ€λμ μ€νλ§ λΆνΈμμ JSON λ°μ΄ν°λ₯Ό μ΅μ ννλ κ°λ¨ν λ°©λ²μ λν΄ μλ €λλ¦¬λ € ν©λλ€. μΉ μλΉμ€μμ μμ μ΅μ νλ μ¬μ©μ κ²½νμ ν° λ³νλ₯Ό κ°μ Έμ¬ μ μκΈ°μ, μ΄λ₯Ό μ νμ©νλ©΄ λ§μ μ΄μ μ μ»μ μ μμ΅λλ€.
π€ μ null κ°μ μ κ±°ν΄μΌ νλμ?
JSON μλ΅ λ°μ΄ν°μμ null κ°μ λ€μκ³Ό κ°μ λ¬Έμ μ μ λ°μμν¬ μ μμ΅λλ€.
- λ°μ΄ν°ν¬κΈ°: λΆνμν null κ°μ μ μ‘λλ λ°μ΄ν°μ ν¬κΈ°λ₯Ό λΆνμνκ² μ¦κ°μν΅λλ€.
- ν΄λΌμ΄μΈνΈ μ²λ¦¬: ν΄λΌμ΄μΈνΈμμλ null κ°μ λν μΆκ°μ μΈ μ²λ¦¬κ° νμνκ² λ©λλ€.
Before
{
"id": 14,
"title": "μ λͺ©",
"content": "λ΄μ©",
"writer": "νκΈΈλ",
"created_id": null
}
π₯Έ Jackson λΌμ΄λΈλ¬λ¦¬λ‘ Null κ° μ κ±°νκΈ°
Spring Bootμμλ Jackson λΌμ΄λΈλ¬λ¦¬λ₯Ό νμ©νμ¬ JSON μλ΅ λ°μ΄ν°λ₯Ό ν¨κ³Όμ μΌλ‘ κ΄λ¦¬ν μ μμ΅λλ€. μλμ μ€μ μ μ¬μ©νλ©΄, null κ°μ κ°μ§ νλλ JSON μλ΅μμ μλμΌλ‘ μ μΈλ©λλ€.
package com.board.backend.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public ObjectMapper objectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return objectMapper;
}
}
μ μ© νμ κ²°κ³Ό μ λλ€.
After
{
"id": 14,
"title": "μ λͺ©",
"content": "λ΄μ©",
"writer": "νκΈΈλ"
}
π€ λ§μΉλ©°
JSON λ΄μ null κ°μ μ κ±°νλ κ²μ κ°λ¨ν μ μ°¨μ²λΌ λ³΄μΌ μ μμ§λ§, μ΄λ¬ν μμ μ΅μ νκ° μλΉμ€μ μλ΅ μκ° κ°μ κ³Ό ν΄λΌμ΄μΈνΈμ μ²λ¦¬ λΆλ΄ κ°μμ ν° λμμ μ€λλ€. νΉν, λμ©λ λ°μ΄ν°λ₯Ό λ€λ£¨λ μλΉμ€λ 리μμ€κ° μ νλ λͺ¨λ°μΌ νκ²½μμλ μ΄λ° μ΅μ νκ° λμ± μ€μν©λλ€.
λ°μ΄ν°λ₯Ό κΉλνκ² κ΄λ¦¬νκ³ , μ¬μ©μμκ² νμν μ 보λ§μ ν¨μ¨μ μΌλ‘ μ λ¬νλ κ²μ μ’μ μΉ μλΉμ€μ κΈ°λ³Έμ λλ€. μ΄ κΈμ ν΅ν΄ μκ°λ λ°©λ²μ΄ μ¬λ¬λΆμ μλΉμ€μ μ μ©λμ΄, μ¬μ©μ κ²½νμ ν λ¨κ³ λ ν₯μμν€λ λ° λμμ΄ λκΈ°λ₯Ό λ°λλλ€.
'π» νλ‘κ·Έλλ° > π Spring' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Springμμ μλͺ»λ λ©μμ§ μμ² νΈλ€λ§νκΈ° (0) | 2023.08.19 |
---|---|
Spring Boot μμ μλ΅ λ° μμΈ μ²λ¦¬ λͺ¨λΈ ꡬμΆνκΈ° (0) | 2023.08.14 |
MyBatis XML Mapper μμ Java λ΄λΆ ν΄λμ€ μ°Έμ‘°νκΈ° (0) | 2023.08.13 |