The javax.json.bind.JsonbException: Cannot create instance of a class: class, No default constructor found. exception occurs when no default constructor is found in the java bean class. The spring boot default implementation for json binding api is org.eclipse.yasson. If no default constructor is found, the yasson code will throw this exception javax.json.bind.JsonbException: Cannot create instance of a class: class, No default constructor found. because the java bean class instance cannot be created.
The error is due to being unable to locate the java class’s default constructor. The java bean is instantiate using the default constructor. If the parser could not locate the default constructor, the instance of the class could not be created. Hence, the exception javax.json.bind.JsonbException: Cannot create instance of a class: class, No default constructor found. will be thrown. We’ll see in the post about this case, how to correct the exception.
Exception
2020-03-14 22:45:50.375 ERROR 29279 --- [ main] o.eclipse.yasson.internal.Unmarshaller : Cannot create instance of a class: class com.yawintutor.Student, No default constructor found.
Exception in thread "main" javax.json.bind.JsonbException: Cannot create instance of a class: class com.yawintutor.Student, No default constructor found.
at org.eclipse.yasson.internal.serializer.ObjectDeserializer.getInstance(ObjectDeserializer.java:101)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:65)
at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:66)
at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:52)
at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:59)
at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:66)
at com.yawintutor.SpringBootJsonbSimpleApplication.JSONToJavaObject(SpringBootJsonbSimpleApplication.java:66)
at com.yawintutor.SpringBootJsonbSimpleApplication.main(SpringBootJsonbSimpleApplication.java:19)
Root Cause
The Yasson package attempts to build a class instance while it attempts to convert a json string to an object in java. First the instance of the java object is generated using the default constructor. All attributes of the json string are assigned with value.
If the default constructor is not available, or if the default constructor does not offer sufficient privilege to call while constructing the instance, the spring.boot application will throw this exception. The default constructor is overwritten with one or more customized constructors.
How to reproduce this issue
Build a java bean class in the spring-boot program. The default constructor may be modified to a parameterized constructor. Now, a class instance is not generated by default constructor. The json binding api must search for the default constructor, this exception will be thrown by the spring boot application, since it is not available.
The example below includes a parameterized int-value constructor. There is no available default constructors.
Student.java
package com.yawintutor;
public class Student {
private int id;
private String name;
public Student(int id) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Solution 1
In the above example, create a default constructor. Other wise delete the constructor which is parameterized. That will solve the problem. The example below shows the default java bean class with a default constructor in-build.
Student.java
package com.yawintutor;
public class Student {
private int id;
private String name;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Solution 2
If the default constructor could not be added to the java bean class, you can create a new java bean class that extends the java bean class. In the default constructor you can use keyword super to assign the default value to the constructor method. The new java bean class should be instantiated in the code.
Student.java
package com.yawintutor;
public class Student {
private int id;
private String name;
public Student(int id) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
CollegeStudent.java
package com.yawintutor;
public class CollegeStudent extends Student {
public CollegeStudent() {
super(0);
}
}
Solution 3
More details on how to use json binding api are explained in the links below.