Java - Jackson Annotations处理没有合适的构造函数 [英] Java - Jackson Annotations to handle no suitable constructor

查看:477
本文介绍了Java - Jackson Annotations处理没有合适的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson ObjectMapper来阅读我的回复。我也使用Spring ResponseErrorHandler来处理响应:

I am using Jackson ObjectMapper to read my response. Also I am using Spring ResponseErrorHandler to handle that response:

@Override
public void handleError(final ClientHttpResponse response) throws IOException {
objectMapper.readValue(response.getBody(), ServiceError.class);
}

我知道问题可以通过添加默认构造函数来解决,但我可以'那样做。我根本无法更改ServiceError类。

I know that problem can by solved by adding default constructor, but I can't do that. I can't change ServiceError class at all.

错误得到的是这个:

No suitable constructor found for type ... can not instantiate fromJSON object(need to add/enable type information?)

有没有任何杰克逊注释都支持这样的问题吗?

Is there are any Jackson annotations to support such issue?

推荐答案

是的,如果有问题的类有合适的构造函数可供使用,你可以使用 @JsonCreator 表示要使用该构造函数。此外,如果它需要多个参数,则需要添加 @JsonProperty 以指示应将哪个JSON属性映射到哪个参数。

Yes, if the class in question has suitable constructor to use, you can use @JsonCreator to indicate that that constructor is to be used. Also, if it takes multiple arguments, you will need to add @JsonProperty to indicate which JSON property should be mapped to which argument.

这篇关于Java - Jackson Annotations处理没有合适的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆