JSON binding api specifies the specifications to convert java object to json string format and converting json string to java objects in spring boot. In spring boot, we see how to convert java object to json string, also we see in spring boot about JSON-BINDING (Jsonb), which is the standard for working between the java object and the json. Json binding (Json-b) has recently been introduced to java as part of the JSR 367 specification.

It is necessary to convert a java object to a json string in the spring boot application. Using a json string, a java object can be stored in a persistence layer like a database. The json string is a text that can be quickly transmitted across the network. In this post, we’ll see how to convert a java bean object to a json string.

The javax.json.bind dependency on the spring boot attaches the dependent jars to the spring boot project. Here we see the step-by-step process for building a spring boot application that converts a java object to a json string.

Step 1 – Create pom.xml with dependency

The first step is to build a spring boot application. The spring boot framework includes two simple “spring-boot-starter” and “spring-boot-starter-test” dependencies. To add json binding jars to the project, add “javax.json.bind-api” dependency to the pom.xml file. This is a specification api jar that enforces all json binding specifications.

Add the json binding jars to the spring boot program. The default json binding is org.eclipse.yasson. Add “org.eclipse.yasson” and “org.glassfish.javax.json” to the pom.xml file.

The pom.xml file below includes all the dependency for json binding.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot Project</description>










Step 2 – Create Java Bean Object

Build a java bean object that will be transformed to a json object in the spring boot framework. The Java Bean object will contain all private variables and public getter and setter methods. The java bean object must have a default constructor.

The example below shows a sample of the java bean model.


package com.yawintutor;

public class Student {

	private int id;

	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;


Step 3 – JSON Binding API code to convert JSON String

The json binding api supports to create a json string by taking input as a java object. The example below demonstrates how to build a java bean object and how to convert a java bean object to a json string.


package com.yawintutor;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootJsonbSimpleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootJsonbSimpleApplication.class, args);


	public static void JavaObjecttoJSON() {
		Student student = new Student();

		Jsonb jsonb = JsonbBuilder.create();
		String jsonString = jsonb.toJson(student);
		System.out.println("Json format String : " + jsonString);


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.2.5.RELEASE)

2020-03-14 17:39:18.695  INFO 1335 --- [           main] c.y.SpringBootJsonbSimpleApplication     : Starting SpringBootJsonbSimpleApplication on banl1691b9157 with PID 1335 (/Users/yawin/STS/workspace/SpringBootJsonbSimple/target/classes started by Yawin in /Users/yawin/STS/workspace/SpringBootJsonbSimple)
2020-03-14 17:39:18.698  INFO 1335 --- [           main] c.y.SpringBootJsonbSimpleApplication     : No active profile set, falling back to default profiles: default
2020-03-14 17:39:19.104  INFO 1335 --- [           main] c.y.SpringBootJsonbSimpleApplication     : Started SpringBootJsonbSimpleApplication in 0.644 seconds (JVM running for 3.197)
Json format String : {"id":100,"name":"Yawin"}

Leave a Reply