I don't think you can do it automatically. You will have to create a static method for that:
public static Status fromString(String string) {
for (Status status : values()) {
if (status.statusStr.equals(string)) {
return status;
}
}
throw new IllegalArgumentException(string);
}
Incorporating @Pshemo's suggestions, the code could also be:
@RequiredArgsConstructor @Getter
public enum Status {
CREATED("created"), IN_PROGRESS("inProgress"), COMPLETED("completed");
private static final Map<String, Status> MAP = Arrays.stream(Status.values())
.collect(Collectors.toMap(Status::getStatusStr, Function.identity()));
private final String statusStr;
public static Status fromString(String string) {
return Objects.requireNonNull(MAP.get(string));
}
}