【问题标题】:Failed to instantiate com.fasterxml.jackson.databind.node.ObjectNode using constructor NO_CONSTRUCTOR with arguments In MongoDB无法在 MongoDB 中使用带有参数的构造函数 NO_CONSTRUCTOR 实例化 com.fasterxml.jackson.databind.node.ObjectNode
【发布时间】:2017-04-07 03:13:43
【问题描述】:

我正在使用 JsonNode 从任何一种 jason 格式获取数据并将其存储到 mongoDb 但是在从 mongoDB 获取数据时,它会抛出如下错误。

Failed to instantiate com.fasterxml.jackson.databind.node.ObjectNode using constructor NO_CONSTRUCTOR with arguments

下面是我的域类

public class Profiler {

 @Id
 private String id;

@Field("email")
private String email;

@Field("profiler")
private Map<String,JsonNode> profiler;

public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public Map<String, JsonNode> getProfiler() {
    return profiler;
}
public void setProfiler(Map<String, JsonNode> profiler) {
    this.profiler = profiler;
}
public Profiler(String email,Map<String,JsonNode> profiler){
    this.email=email;
    this.profiler = profiler;
}
@JsonCreator
public Profiler(@JsonProperty("_id")String id,@JsonProperty("email")String email,@JsonProperty("profiler")Map<String,JsonNode> profiler){
    this.id=id;
    this.email=email;
    this.profiler = profiler;
}
public Profiler(String id){
    this.id=id;
}
public Profiler(Map<String,JsonNode> profiler){
    this.profiler = profiler;
}
public Profiler(){

}

}



public interface ProfilerRepository extends MongoRepository<Profiler, String>{
public Profiler findOneByEmail(String email);
}

我的控制器调用如下,我在这一行收到错误。

Profiler profile=profileService.findOneByEmail(email);

【问题讨论】:

  • 你能出示你的 mongo 文件吗?

标签: json spring mongodb


【解决方案1】:

在我的情况下,问题解决了。我有我定义的实体:

private JsonNode data;

我改成了:

private Map<String,String> data;

或者这也可以:

   private Map<Object,String> data;

如果您有任何问题,请告诉我

【讨论】:

    【解决方案2】:

    我已进行此更改并按预期工作。

    Map<String, Object> profile;
    

    【讨论】:

    • 你打错了:profile -> profiler
    【解决方案3】:

    出现此问题是因为com.fasterxml.jackson.databind.node.ObjectNode 类没有默认构造函数(无参数构造函数),Jackson 需要默认构造函数。

    Related Post refer azerafati's answer

    如果在域类中将profiler 字段定义为静态,则问题可以解决。

    private static Map<String, JsonNode> profiler;
    

    请注意,静态字段有其自身的限制和问题。我可以保证这将解决上述异常。但是,这可能不是最合适的解决方案。

    【讨论】:

    • 我尝试了同样的方法,但它不起作用。但是我做了一个更改而不是 JsonNode 我使用了像 Map 分析器这样的对象;并按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多