javax.json.bind.JsonbException: Cannot create instance of a class: class, No default constructor found.

For json binding api the default implementation is org.eclipse.yasson. The yasson code throws this exception javax.json.bind.JsonbException: Cannot create instance of a class: class com.yawintutor.Student, No default constructor found. if it can not construct a java bean class instance because no default constructor is available.

The error is due to being unable to locate the java class’s default constructor. We’ll see in the post about this case, how to correct the 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

More details on how to use json binding api are explained in the links below.




Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *