[BAEL-12185] - Updated JACKSON ANNOTATIONS article

This commit is contained in:
amit2103 2019-03-03 01:33:22 +05:30
parent 5501eed39e
commit 2b8ebe7de2
4 changed files with 74 additions and 2 deletions

View File

@ -13,7 +13,7 @@ import java.util.List;
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonRootName("writer")
@JsonRootName(value = "writer", namespace = "book")
public class Author extends Person {
List<Item> items = new ArrayList<>();

View File

@ -0,0 +1,31 @@
package com.baeldung.jackson.annotation;
import com.fasterxml.jackson.annotation.JsonAlias;
public class AliasBean {
@JsonAlias({ "fName", "f_name" })
private String firstName;
private String lastName;
public AliasBean() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

View File

@ -14,6 +14,7 @@ import java.util.TimeZone;
import org.junit.Test;
import com.baeldung.jackson.annotation.AliasBean;
import com.baeldung.jackson.annotation.BeanWithCreator;
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
import com.baeldung.jackson.annotation.BeanWithFilter;
@ -38,6 +39,7 @@ import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
import com.baeldung.jackson.exception.UserWithRoot;
import com.baeldung.jackson.jsonview.Item;
import com.baeldung.jackson.jsonview.Views;
import com.baeldung.jackson.serialization.jsonrootname.Author;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.MapperFeature;
@ -46,6 +48,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class JacksonAnnotationUnitTest {
@ -372,5 +375,43 @@ public class JacksonAnnotationUnitTest {
assertThat(result, containsString("1"));
assertThat(result, containsString("name"));
}
@Test
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
// arrange
String json = "{\"fName\": \"Alex\", \"lastName\": \"Theedom\"}";
// act
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
// assert
assertThat(aliasBean.getFirstName(), is("Alex"));
}
@Test
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
// arrange
Author author = new Author("Alex", "Theedom");
// act
ObjectMapper mapper = new XmlMapper();
mapper = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).enable(SerializationFeature.INDENT_OUTPUT);
String result = mapper.writeValueAsString(author);
// assert
assertThat(result, containsString("<writer xmlns=\"book\">"));
/*
<writer xmlns="book">
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
<firstName xmlns="">Alex</firstName>
<lastName xmlns="">Theedom</lastName>
<items xmlns=""/>
</writer>
*/
}
}

View File

@ -1630,7 +1630,7 @@
<javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
<jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>
<jstl.version>1.2</jstl.version>
<jackson.version>2.9.7</jackson.version>
<jackson.version>2.9.8</jackson.version>
<commons-fileupload.version>1.3</commons-fileupload.version>
<junit-platform.version>1.2.0</junit-platform.version>
<junit-jupiter.version>5.2.0</junit-jupiter.version>