The javax.json.bind.JsonbException: JSON Binding provider org.eclipse.yasson.JsonBindingProvider not found exception occurs when the java.json.bind maven dependency is added and JSON bind implementation dependency is not added in the spring boot maven. As the implemented class is not available in the class path, the exception java.lang.ClassNotFoundException: org.eclipse.yasson.JsonBindingProvider will be thrown.
The exception “JSON Binding Provider org.eclipse.yasson.JsonBindingProvider not found” will be thrown at the startup of the application. Recently, JSON processing is supported in Java. JSON Binding API helps to process JSON and Java Objects. Here, we will see what this exception is.
The JSON bind object is added to the spring boot application. The Javax JSON bind package is a specification for JSON support. If the package is added to the spring boot application, the exception below will be thrown at the startup of the application.
Exception
Exception in thread "main" javax.json.bind.JsonbException: JSON Binding provider org.eclipse.yasson.JsonBindingProvider not found
at javax.json.bind.spi.JsonbProvider.provider(JsonbProvider.java:120)
at javax.json.bind.JsonbBuilder.create(JsonbBuilder.java:108)
at com.yawintutor.SpringBootJsonbSimpleApplication.main(SpringBootJsonbSimpleApplication.java:27)
Caused by: java.lang.ClassNotFoundException: org.eclipse.yasson.JsonBindingProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javax.json.bind.spi.JsonbProvider.provider(JsonbProvider.java:117)
... 2 more
Root Cause
In the spring boot application, the javax json bind api dependency is added to manipulate the json string to the java objects. This exception will be thrown at the startup of the application after the dependency has been added.
The javax.json.bind dependency jar is the java specification for the json api processing. There is no implementation added in this package. Two common implementation packages are available. Yasson of org.eclipse and Apache Johnzon.
Any of these two package dependencies should be added to the spring boot application. If the dependency on the application is added, this exception will be resolved.
How to reproduce this issue
Create a spring boot application in the Spring Tool Suite. Add the dependency of the javax.json.bind to the pom.xml file. This will add all the dependent jars to the APIs for json processing. Create a java bean class that contains getter and setter methods. Add the json-related annotation for each private variable. Add a simple code to convert from java bean object to JSON string.
The spring boot application would fail by throwing an exception at the startup of the application.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.yawintutor</groupId>
<artifactId>Spring-Application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootJsonbSimple</name>
<description>Spring Boot Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Student.java
package com.yawintutor;
import javax.json.bind.annotation.JsonbProperty;
public class Student {
@JsonbProperty("studentId")
private int id;
@JsonbProperty("studentName")
private String name;
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;
}
}
SpringBootJsonbSimpleApplication.java
package com.yawintutor;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJsonbSimpleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJsonbSimpleApplication.class, args);
Student student = new Student();
student.setId(100);
student.setName("Yawin");
Jsonb jsonb = JsonbBuilder.create();
String jsonString = jsonb.toJson(student);
System.out.println("Json format String : "+jsonString);
}
}
Solution 1
The api dependency of javax.json.bind is connected to org.eclipse.yasson as a default implementation. When the program starts, the default implementation provider class is searched for. The default provider is not added as a dependency, the json apis can not be loaded by the application. In order to resolve this exception, the following two dependency must be added in pom.xml.
pom.xml
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>provided</scope>
</dependency>
The complete pom.xml file is shown as like below. the pom.xml will contain 3 dependency to support json processing using javax.json.bind api calls.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.yawintutor</groupId>
<artifactId>Spring-Application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootJsonbSimple</name>
<description>Spring Boot Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Solution 2
The other implementation from Apache can be used to resolve this issue. The code below should be added in the pom.xml file to use Apache Johnzon dependency.
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-jsonb</artifactId>
<version>1.1.4</version>
</dependency>