Spring boot GSON Integration
Spring Boot provides integration with:
Jackson
GSON
JSON-B
Spring support Jackson, Gson and JSON-B all at the same time, you can Autowire the bean as needed, The below example shows, how to autowire GSON object.
Configure Dependency (pom.xml)
If you add GSON in the classpath, Spring bean will be automatically configured.
You can exclude Jackson from spring-boot-starter-web if you need only GSON as default.
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.candidjava</groupId> <artifactId>spring</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> </dependencies> <build> <finalName>springBoot</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Using GSON in Controller (StudentController.java)
You can autowire the GSON bean if both GSON and Jackson are in the dependency.
If you have excluded the Jackson then spring will use GSON as default, there is no need for auto wiring it.
package com.candidjava.springboot.controller; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; @RestController @RequestMapping(value = "/student") public class StudentConroller { @Autowired StudentService service; @Autowired Gson gson; @PostMapping("/create") public void create(@Valid @RequestBody Student student) { service.createStudent(student); } @GetMapping("/getAll") public String get() { return gson.toJson(service.getAllStudents()); // using Gson } @GetMapping("/get/{id}") public String getById(@PathVariable("id") String id) throws JsonProcessingException { Student student = service.getStudentById(id); ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(student); // using jackson } @PutMapping("/update/{id}") public void update(@PathVariable("id") String id, @Valid @RequestBody Student student) { service.updateStudent(id, student); } @DeleteMapping("/delete/{id}") public void deleteById(@PathVariable("id") String id) { this.service.deleteStudentById(id); } }
StudentService.java
package com.candidjava.springboot.service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Service; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; @Service public class StudentService { private List<Student> studentList = new ArrayList<Student> (Arrays.asList( new Student("1", "ram", "20"), new Student("2", "arun", "21"), new Student("3", "karthick", "22") )); public void createStudent(Student student) { studentList.add(student); } public List<Student> getAllStudents() { return studentList; } public Student getStudentById(String id) { return studentList.stream().filter(student -> student.getId().equals(id)).findFirst().get(); } public void updateStudent(String id, Student student) { int counter = 0; for (Student eachStudent: studentList) { if (eachStudent.getId().equals(id)) { studentList.set(counter, student); } counter++; } } public void deleteStudentById(String id) { studentList.removeIf(student -> student.getId().equals(id)); } }
Student.java
package com.candidjava.springboot.models; public class Student { private String id; private String name; private String age; public Student(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
application.yml
server: port : 9090
Launch as Spring boot application (SpringApplication.java)
package com.candidjava.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringApplication { public static void main(String[] args) { SpringApplication.run(SpringApplication.class, args); } }
Download
Download source code from my github account Click here