Configuring Spring Boot to use Gson instead of Jackson

This commit is contained in:
Umesh Awasthi
2019-01-01 15:07:55 -08:00
parent a5cecec5c4
commit 9e8e3c1e8d
9 changed files with 598 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.javadevjournal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@SpringBootApplication
public class GsonSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(GsonSpringBootApplication.class, args);
}
}

View File

@@ -0,0 +1 @@
spring.http.converters.preferred-json-mapper=gson

View File

@@ -0,0 +1,54 @@
package com.javadevjournal;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.javadevjournal.data.Product;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GsonSpringBootApplicationTests {
private Product product;
@Before
public void setup(){
product =new Product("123","Demo Product",123);
}
@Test
public void simpleGsonTest() throws JSONException {
String expected = "{\n" +
"\"code\": \"123\",\n" +
"\"name\": \"Demo Product\",\n" +
"\"price\": 123\n" +
"}";
Gson gson = new GsonBuilder().create();
String data= gson.toJson(product);
JSONAssert.assertEquals(expected,data,false);
}
@Test
public void errorGsonTest() throws JSONException {
String expected = "{\n" +
"\"code\": \"1233\",\n" +
"\"name\": \"Demo Product\",\n" +
"\"price\": 123\n" +
"}";
Gson gson = new GsonBuilder().create();
String data= gson.toJson(product);
JSONAssert.assertEquals(expected,data,false);
}
}