Spring 4 MVC GsonHttpMessageConverter Example with configureMessageConverters()

By Arvind Rai, November 06, 2015
On this page, we will provide Spring 4 MVC GsonHttpMessageConverter example with configureMessageConverters() method in JavaConfig. GsonHttpMessageConverter uses Google Gson library to convert to and from JSON and java object. When we use GsonHttpMessageConverter, the default spring converter for JSON and java object is changed and becomes Google Gson library. Spring has introduced GsonHttpMessageConverter in Spring 4.1. Here we will provide a complete example step by step.

Software Used

We are using following software in our example.
1. Java 8
2. Spring 4.1
3. Gradle
4. Eclipse
5. Tomcat 8

Gradle File

To enable GsonHttpMessageConverter, we are using gson-2.4-sources.jar.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'spring4'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE'
    compile 'com.google.code.gson:gson:2.4'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.2.3.RELEASE'
}  

JavaConfig: Configure GsonHttpMessageConverter using configureMessageConverters()


AppConfig.java
package com.concretepage.config;  
import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@Configuration 
@ComponentScan("com.concretepage") 
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter  {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    	GsonHttpMessageConverter msgConverter = new GsonHttpMessageConverter();
        Gson gson = new GsonBuilder().setPrettyPrinting().create(); 
    	msgConverter.setGson(gson);
        converters.add(msgConverter);
    }
} 

configureMessageConverters(): It configures the HttpMessageConverter. If no converter is added, default list of converters are registered automatically.
GsonHttpMessageConverter: It can read and write JSON using Google Gson library.

Controller and Java Bean


BlogController.java
package com.concretepage.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.concretepage.bean.Writer;
@RestController
public class BlogController {
	@Autowired
	private Writer writer;
	@RequestMapping(value = "/blog")
	public List<Writer> getBlog() {
	    return writer.getWriters();
	}
} 

Writer.java
package com.concretepage.bean;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
public class Writer {
	private String name;
	private String location;
	public Writer(){}
	public Writer(String name, String location) {
		this.name = name;
		this.location = location;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public List>Writer< getWriters() {
                List<Writer> list = new ArrayList<>();
                list.add(new Writer("Ram","Ayodhya"));
                list.add(new Writer("Krishna","Mathura"));
                list.add(new Writer("Shankar","Himalaya"));
		return list;
	}
} 

Output

Use the URL http://localhost:8080/spring4-1/blog
1. Output in console
17:52:34.787 [http-nio-8080-exec-4] DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor - Written [[com.concretepage.bean.Writer@7442d900, com.concretepage.bean.Writer@707d5350, com.concretepage.bean.Writer@357fd33b]] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.GsonHttpMessageConverter@b6f5aad]
 
2. Output in browser
[
  {
    "name": "Ram",
    "location": "Ayodhya"
  },
  {
    "name": "Krishna",
    "location": "Mathura"
  },
  {
    "name": "Shankar",
    "location": "Himalaya"
  }
] 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us