I've a Project class with these attributes
private String id;
private String name;
private ProjectStatus status;
Projectstatus is an enum like this:
public enum ProjectStatus {
I,
A,
C,
OPEN,
CLOSED
}
I'm calling a remote rest service which responds with json to pull information about the project.
The issue I'm having is that the status in the remote service payload is 'Open' which causes an issue while trying to create the Project object as the status in my ProjectStatus enum is OPEN, all uppercase.
I could create the ProjectStatus like this to match the response from the service:
public enum ProjectStatus {
I,
A,
C,
Open,
Closed
}
However, I'd be violating the standard to name enums.
I've also tried to do the following without success
public enum ProjectStatus {
I,
A,
C,
OPEN("Open"),
CLOSED("Closed")
}
What's the right way to go here?
CodePudding user response:
Easies solution would be defining Value to what will be serialized via @JsonValue
Annotation. Something along this lines
public enum MySuperEnum {
VALUE_1("value1", "Value 1 Desc"),
VALUE_2("valu2", "Value 2 Desc"),
VALUE_3("value3", "Value 3 Desc")
;
private final String code;
private final String desc;
MySuperEnum(String code, String desc){
this.code = code;
this.desc = desc;
}
@JsonValue
public String getCode() {
return code;
}
public String getDesc(){
return desc;
}
}
CodePudding user response:
I got it to work like this
public enum ProjectStatus {
I("I"),
A("A"),
C("C"),
OPEN("Open"),
CLOSED("Closed");
private String value;
ProjectStatus(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
@JsonCreator
public static ProjectStatus fromString(String value) {
return ProjectStatus.valueOf(value.toUpperCase());
}
}