JsonParse Error - @JsonCreater
Today's μ½μ§
07/18/2021
βERROR
μ€νλ§ AOPλ₯Ό ꡬννλ κ³Όμ μμ AOP λ¨μ λλ¬νκΈ° μ λΆν° json νμ± μλ¬κ° λ°μνλ€.
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `noums.study.pizzahouse.enums.FoodMenu` from String "ggg"
μ²μμ μ AOP μ λλ¬λ νκΈ° μ μ json νμ± μλ¬κ° λλκ²μΈμ§ μ΄μ λ₯Ό λͺ°λΌ νμ°Έμ μ½μ§νμλ€.
κ²°λ‘ μ RequestBody μμ Json κ°μ²΄λ₯Ό λ°μμ Enum ν΄λμ€μ λ§€ννλ κ³Όμ μμ λ§€ν μ€λ₯κ° λ°μν κ²μ΄μλ€.
AOP μ μ JSON parse error κ° λλ μ΄μ
μ€νλ§ μ»¨ν μ΄λ μμμ μ€νμμλ₯Ό 보면
dispatcher servlet β interceptor β AOP β Controller
μ΄λ€.
request body λ₯Ό μ½μ΄μμ jsonμ νμ±νλ κ³Όμ μ μμλ
interceptor - requestbody json parsing β AOP
μ΄λ€.
requestλ‘ λ°μμ¨ json κ°μ²΄κ° λ¨Όμ νμ±λκ³ λ λ€μ AOPλ¨μΌλ‘ λμ΄κ°κΈ° λλ¬Έμ, ENUMκ°μ²΄μ λ§μ§μλ κ°μ΄ json κ°μ²΄λ‘ λμ΄κ° κ²½μ° json parse error κ° λλ κ²μ΄λ€.
π‘ν΄κ²°λ°©μ
Enum ν΄λμ€ μμμ @JsonCreater μ΄λ Έν μ΄μ μΌλ‘ μ²λ¦¬λ₯Ό ν΄μ€μΌλ‘μ¨ json νμ±μ 컨νΈλ‘€ ν μ μκ² λλ€.
@JsonCreator public static FoodMenu fromJson(String menu){ System.out.println(">>>>>> JsonCreater :: "+ menu); for (FoodMenu foodMenu : FoodMenu.values()) { if ( foodMenu.toString().equals(menu) ) { System.out.println(">>>>>> correct !"); return foodMenu; } } return null; }
λμ΄μ¨ κ°μ²΄κ° ENUM νλκ°κ³Ό λ§μ§ μμ κ²½μ° null μ return ν΄μ€λ€.