【发布时间】:2019-12-07 06:21:35
【问题描述】:
使用 jackson2.9.8 进行枚举反序列化时遇到问题。同样适用于 Gson。 模型是根据 swagger api 定义自动生成的
使用 Gson,它工作正常。对于 jackson,它不适用于 011、013 和 019,但适用于其他值
Swagger api 定义的sn-p
服务代码: 类型:字符串 枚举: - “001” - “002” - “003” - “004” - “005” - “007” - “008” - “009” - “011” - “013” - “019”
自动生成的代码(为了可读性删除了getter/setter)
public class ProcessErrorTest1 {
static String errorJson =
" {\n" +
" \"timestamp\": \"2019-07-29 11:55:48\",\n" +
" \"serviceCode\": \"019\",\n" +
" \"message\": \"service failed. \",\n" +
" \"rootException\": {\n" +
" \"source\": \"test\",\n" +
" \"reasonCode\": \"2131\",\n" +
" \"reasonText\": \"test\"\n" +
" }\n" +
"}";
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
AppErrorResponse error = mapper.readValue(errorJson, AppErrorResponse.class);
// Gson gson = new Gson();
//AppErrorResponse error = gson.fromJson(errorJson, AppErrorResponse.class);
System.out.println("error::" + error);
}
}
public class AppErrorResponse {
@SerializedName("message")
private String message = null;
@SerializedName("rootException")
private ErrorResponse rootException = null;
/**
* Gets or Sets serviceCode
*/
@JsonAdapter(ServiceCodeEnum.Adapter.class)
public enum ServiceCodeEnum {
_001("001"),
_002("002"),
_003("003"),
_004("004"),
_005("005"),
_007("007"),
_008("008"),
_009("009"),
_011("011"),
_013("013"),
// @JsonProperty("019") if add this , it works fine. But can't modify the auto generated code as it is available as jar
_019("019");
}
@SerializedName("serviceCode")
private ServiceCodeEnum serviceCode = null;
@SerializedName("timestamp")
private String timestamp = null;
}
public class ErrorResponse {
@SerializedName("reasonCode")
private String reasonCode = null;
@SerializedName("reasonText")
private String reasonText = null;
@SerializedName("source")
private String source = null;
}
jersey1:jackson 生成的代码
import java.util.Objects;
import com.bt.consumer.appointment.dto.MAC2ErrorResponse;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
public class AppErrorResponse {
@JsonProperty("message")
private String message = null;
@JsonProperty("rootException")
private ErrorResponse rootException = null;
public enum ServiceCodeEnum {
_001("001"),
_002("002"),
_003("003"),
_004("004"),
_005("005"),
_007("007"),
_008("008"),
_009("009"),
_011("011"),
_013("013"),
_019("019");
private String value;
ServiceCodeEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static ServiceCodeEnum fromValue(String text) {
for (ServiceCodeEnum b : ServiceCodeEnum.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
@JsonProperty("serviceCode")
private ServiceCodeEnum serviceCode = null;
@JsonProperty("timestamp")
private String timestamp = null;
}
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
public class ErrorResponse {
@JsonProperty("reasonCode")
private String reasonCode = null;
@JsonProperty("reasonText")
private String reasonText = null;
@JsonProperty("source")
private String source = null;
}
除了使用 jackson 的 _011、_013 和 _019 之外,它对所有人都适用
错误信息:-
com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“011”反序列化ServiceCodeEnum类型的值:值不是声明的枚举实例名称之一:[_011、_001、_002、_003、_004、_005、_007, _008、_009、_013、_019]
【问题讨论】:
标签: java enums jackson swagger