0

I created an application with springboot+mongoDB. I have a class Test which has an instance variable duration of class "javax.xml.datatype.Duration". Duration is an abstract class. I am able to save data using rest request.But when I am retrieving saved data from DB using get request getting following error.

 {
    "timestamp": "2018-06-05T09:27:28.538+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Failed to instantiate com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl using constructor NO_CONSTRUCTOR with arguments ",
    "path": "/user/test"
}

I have checked the DB to get the BSON representation

{                                                                                                                                                                                                                                                    
  "_id" : "Arjun",
  "duration" : {
    "signum" : 1,
    "hours" : "2",                                                                                                                                                                                                                               
    "minutes" : "0",
    "seconds" : "0",
    "_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl"
  },                                                                                                                                                                                                                                           
  "_class" : "com.runitsimple.WebshopConnectorMangoDB3.bean.Test"
}

Like you can see mongo is mapped to the duration class instance to "com.sun.org.apache.xerces.internal.jaxp.datatype.DurationImpl"
it is a default scope class and we won't able to create the instance outside the package. Really stuck with the problem. Please help. class is below

public class Test {
    @Id
    private String name;
    private Duration duration;

    public Test() {
    }

    public Test(String name, Duration duration) {
        this.name = name;
        this.duration = duration;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Duration getDuration() {
        return duration;
    }

    public void setDuration(Duration duration) {
        this.duration = duration;
    }
}

1 Answers1

0

Using oops concept done a fix. I extended the Test class and override the duration field. Added a new field durationString.

public class Test {
@Id
private String name;
@Transient
@JsonIgnore
private Duration duration;
@JsonProperty("duration")
private Duration durationString;

public Test() {
}

public Test(String name, Duration duration) {
    this.name = name;
    this.duration = duration;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Duration getDuration() {
    return duration;
}

public void setDuration(Duration duration) {
    this.duration = duration;
}}

using @Transient removed duration field going to DB. computed durationString = duration.toString on the fly. Using @JsonIgnore property ignored the duration field and using @JsonProperty("duration") given back the same response which user got previously.

  • DB parsing issue solved.
  • The user will get the same JSON as before.