使用 jackson2.9.8 进行枚举反序列化时出现问题。同样适用于 Gson。模型是从 swagger api 定义自动生成的
使用 Gson,它工作正常。对于 jackson,它不适用于 011、013 和 019,但适用于其他值
Swagger API 定义的片段
服务代码:类型:字符串枚举:-“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;
}
侃侃尔雅
相关分类