Projects/[Spring] Coffee Shop Project

[트러블슈팅] JSON 응답 필드 순서 고정하기 (JsonPropertyOrder)

montmer27 2026. 4. 3. 21:06

상황

OrderMenuResponse의 JSON 응답 필드 순서가 의도한 대로 나오지 않았다. subtotalPrice가 중간에 출력되어 가독성이 떨어졌다.

{
  "menuName": "아메리카노",
  "quantity": 2,
  "subtotalPrice": 9000,
  "unitPrice": 4500
}

해결 방법

@JsonPropertyOrder 어노테이션을 사용하면 JSON 응답의 필드 출력 순서를 고정할 수 있다.

@Getter
@JsonPropertyOrder({"menuName", "unitPrice", "quantity", "subtotalPrice"})
public class OrderMenuResponse {

    private final String menuName;
    private final Long unitPrice;
    private final int quantity;
    private final Long subtotalPrice;

    ...
}

적용 후 응답 필드가 지정한 순서대로 고정된다.

{
  "menuName": "아메리카노",
  "unitPrice": 4500,
  "quantity": 2,
  "subtotalPrice": 9000
}