7

enter image description hereI'm using JsonNode for getting data from any kind of jason format and storing it to mongoDb But while fetching data from mongoDB it is throwing error as below.

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

Below is my domain class

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);
}

And my controller call is as below and I'm getting the error on this line.

Profiler profile=profileService.findOneByEmail(email);
Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40
Bhargav Patel
  • 1,098
  • 10
  • 23

3 Answers3

8

I have made this changes and work as expected.

Map<String, Object> profile;
Bhargav Patel
  • 1,098
  • 10
  • 23
2

This problem occurs because com.fasterxml.jackson.databind.node.ObjectNode class doesn't have default constructor (no argument constructor) and Jackson expects the default constructor.

Related Post refer azerafati's answer

The problem can be resolved if you define the profiler field as static in domain class.

private static Map<String, JsonNode> profiler;

Please note that static fields have its own limitations and issues. I can assure that this would resolve the above exception. However, it may not be the most appropriate solution.

Community
  • 1
  • 1
notionquest
  • 37,595
  • 6
  • 111
  • 105
  • I tried the same but it not working. but i did one changes instead of JsonNode i have used Object like Map profiler; and working as exprected. – Bhargav Patel Nov 29 '16 at 07:39
0

in my case problem solved . i had entity that i defined :

private JsonNode data;

i changed it to:

private Map<String,String> data;

or this also work :

   private Map<Object,String> data;

please let me know if you had any question

mi_mo
  • 135
  • 1
  • 8