How to print enum description

Tags: ,



Im creating a Java application, I used enum to create movie category. When I input MovieCategory.WAR I would like to see War movie(My description) instead of WAR. How is it possible? I tried MovieCategory.WAR.getDescription() but does’t work.

public enum MovieCategory {
   COMEDY("Comedy"), HORROR("Horror"), SCIFI("Sci-Fi"),
   ACTION("Action movie"), ROMANTIC("Romantic"),
   CLASSIC("Classic"), WAR("War movie");

   private final String description;

   MovieCategory(String description) {
       this.description = description;
   }

   public String getDescription() {
       return description;
   }
}
@Service
public class MovieManager {

    private List<Movie> movieList;

    public MovieManager() {
        this.movieList = new ArrayList<>();
        movieList.add(new Movie("City 44",MovieCategory.WAR,"Testing Description",167,12));
    }

    public boolean addMovie(Movie movie){
        return movieList.add(movie);
    }

    public List<Movie> getMovieList() {
        return movieList;
    }

    public void setMovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }
}
@RestController
public class MovieApi {

    private MovieManager movieManager;

    @Autowired
    public MovieApi(MovieManager movieManager) {
        this.movieManager = movieManager;
    }

    @GetMapping("/movies")
    public List<Movie> printMovie(){
        return movieManager.getMovieList();
    }


    @PostMapping("/moreMovies")
    public boolean addMovie(@RequestBody Movie movie){
        return movieManager.addMovie(movie);
    }
}

Answer

The enum works correctly:

  public static void main(String[] args) {
    // Displays: Comedy
    System.out.println(COMEDY.getDescription());
    // Displays: COMEDY
    System.out.println(COMEDY);
  }

Or, maybe you want the toString method to use description?

public enum MovieCategory {
  COMEDY("Comedy");

  private final String description;

  MovieCategory(String description) {
    this.description = description;
  }

  public String getDescription() {
    return description;
  }

  @Override
  public String toString() {
    return getDescription();
  }

  public static void main(String[] args) {
    // Displays: Comedy
    System.out.println(COMEDY.getDescription());
    // Displays: Comedy
    System.out.println(COMEDY);
  }
}

UPDATE #3: It seems the issue is the JSON response. If you want the description to be returned, you can annotate the getDescription method wtih @JsonValue.

import com.fasterxml.jackson.annotation.JsonValue;

public enum MovieCategory {
  WAR("War movie");

  private final String description;

  MovieCategory(String description) {
    this.description = description;
  }

  @JsonValue
  public String getDescription() {
    return description;
  }

  @Override
  public String toString() {
    return getDescription();
  }

  public static void main(String[] args) {
    // Displays: Comedy
    System.out.println(WAR.getDescription());
    // Displays: Comedy
    System.out.println(WAR);
  }
}


Source: stackoverflow