enum에서 한글사용하기

catch me if u can!·2021년 6월 29일
1
  1. enum형식 정의 시, 한글설명을 추가한다.
  2. nameOf는 한글명을 파라미터로 받아서 enum 형식으로 반환한다.
  3. JSON으로 변환시 한글명이 반환되도록 하기 위해, @JsonFormat 어노테이션을 지정한다.
import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum CustomStatus {
  HIGH("상"),
  MEDIUM("중"),
  LOW("하");
  
  final private String name;
  private CustomStatus(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }

  public static CustomStatus nameOf(String name) {
    for (CustomStatus status : CustomStatus.values()) {
      if (status.getName().equals(name)) {
        return status;
      }
    }
    return null;
  }
}
profile
마쿠투소케 난쿠로나이사

0개의 댓글