Skip to content

Commit f915251

Browse files
committed
Ignore Unknown Properties or New Fields with Jackson
1 parent 5eb07d3 commit f915251

File tree

7 files changed

+329
-0
lines changed

7 files changed

+329
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.howtoprogram</groupId>
7+
<artifactId>ignore-unknown-properties-jackson</artifactId>
8+
<version>0.1.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.1.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-databind</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.howtoprogram.jackson;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.web.client.RestTemplateBuilder;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.web.client.RestTemplate;
11+
12+
@SpringBootApplication
13+
public class Application {
14+
15+
private static final Logger log = LoggerFactory.getLogger(Application.class);
16+
17+
public static void main(String args[]) {
18+
SpringApplication.run(Application.class);
19+
}
20+
21+
@Bean
22+
public RestTemplate restTemplate(RestTemplateBuilder builder) {
23+
return builder.build();
24+
}
25+
26+
@Bean
27+
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
28+
return args -> {
29+
Quote quote = restTemplate.getForObject(
30+
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
31+
log.info(quote.toString());
32+
};
33+
}
34+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.howtoprogram.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonAnySetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
6+
public class Book {
7+
8+
private Long id;
9+
private String name;
10+
private String author;
11+
// getters and setters
12+
13+
// @JsonAnySetter
14+
// public void handleUnknown(String key, Object value) {
15+
// System.out.print("property not bound:" + key);
16+
// }
17+
18+
public Book() {
19+
20+
}
21+
22+
23+
24+
public Book(Long id, String name, String author) {
25+
super();
26+
this.id = id;
27+
this.name = name;
28+
this.author = author;
29+
}
30+
31+
/**
32+
* @return the id
33+
*/
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
/**
39+
* @param id the id to set
40+
*/
41+
public void setId(Long id) {
42+
this.id = id;
43+
}
44+
45+
/**
46+
* @return the name
47+
*/
48+
public String getName() {
49+
return name;
50+
}
51+
52+
/**
53+
* @param name the name to set
54+
*/
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
/**
60+
* @return the author
61+
*/
62+
public String getAuthor() {
63+
return author;
64+
}
65+
66+
/**
67+
* @param author the author to set
68+
*/
69+
public void setAuthor(String author) {
70+
this.author = author;
71+
}
72+
73+
/*
74+
* (non-Javadoc)
75+
*
76+
* @see java.lang.Object#toString()
77+
*/
78+
@Override
79+
public String toString() {
80+
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
81+
}
82+
83+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.howtoprogram.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
public class Quote {
6+
7+
private String type;
8+
private Value value;
9+
10+
11+
public Quote() {
12+
}
13+
14+
public String getType() {
15+
return type;
16+
}
17+
18+
public void setType(String type) {
19+
this.type = type;
20+
}
21+
22+
public Value getValue() {
23+
return value;
24+
}
25+
26+
public void setValue(Value value) {
27+
this.value = value;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Quote{" +
33+
"type='" + type + '\'' +
34+
", value=" + value +
35+
'}';
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.howtoprogram.jackson;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties(ignoreUnknown = true)
6+
public class Value {
7+
8+
private Long id;
9+
private String quote;
10+
11+
public Value() {
12+
}
13+
14+
public Long getId() {
15+
return this.id;
16+
}
17+
18+
public String getQuote() {
19+
return this.quote;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public void setQuote(String quote) {
27+
this.quote = quote;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Value{" +
33+
"id=" + id +
34+
", quote='" + quote + '\'' +
35+
'}';
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=false
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.howtoprogram.jackson;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.fasterxml.jackson.databind.DeserializationFeature;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.junit.Assert;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
import org.springframework.web.client.RestTemplate;
31+
32+
import java.io.IOException;
33+
34+
@RunWith(SpringRunner.class)
35+
@SpringBootTest
36+
public class ApplicationTest {
37+
38+
@Autowired
39+
private RestTemplate restTemplate;
40+
41+
@Test
42+
public void contextLoads() {
43+
assertThat(restTemplate).isNotNull();
44+
}
45+
46+
@Autowired
47+
private ObjectMapper objectMapper;
48+
49+
@Test
50+
public void testIgnoreUnknownFields() {
51+
52+
ObjectMapper objectMapper = new ObjectMapper();
53+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
54+
String json = "{\"id\": 1,\"name\": \"Core Java\",\"author\": \"Cay Hor.\",\"qty\": 2," +
55+
"\"rating\": 3.8}";
56+
57+
try {
58+
Book javaBook = objectMapper.readValue(json, Book.class);
59+
60+
assertNotNull(javaBook);
61+
62+
} catch (IOException e) {
63+
e.printStackTrace();
64+
}
65+
66+
}
67+
68+
@Test
69+
public void testIgnoreUnknownFieldsGlobally() {
70+
71+
//ObjectMapper objectMapper = new ObjectMapper();
72+
//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73+
String json = "{\"id\": 1,\"name\": \"Core Java\",\"author\": \"Cay Hor.\",\"qty\": 2," +
74+
"\"rating\": 3.8}";
75+
76+
try {
77+
Book javaBook = objectMapper.readValue(json, Book.class);
78+
79+
assertNotNull(javaBook);
80+
81+
} catch (IOException e) {
82+
e.printStackTrace();
83+
}
84+
85+
}
86+
87+
}

0 commit comments

Comments
 (0)