Merge pull request #6503 from eugenp/BAEL-12185-v2
new example for jackson article
This commit is contained in:
commit
53f4bbf651
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.jackson.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
|
||||||
|
@JsonRootName(value = "user", namespace="users")
|
||||||
|
public class UserWithRootNamespace {
|
||||||
|
public int id;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public UserWithRootNamespace() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserWithRootNamespace(final int id, final String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -37,6 +37,7 @@ import com.baeldung.jackson.date.EventWithSerializer;
|
||||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
||||||
import com.baeldung.jackson.exception.UserWithRoot;
|
import com.baeldung.jackson.exception.UserWithRoot;
|
||||||
|
import com.baeldung.jackson.exception.UserWithRootNamespace;
|
||||||
import com.baeldung.jackson.jsonview.Item;
|
import com.baeldung.jackson.jsonview.Item;
|
||||||
import com.baeldung.jackson.jsonview.Views;
|
import com.baeldung.jackson.jsonview.Views;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
@ -47,6 +48,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||||
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||||
|
|
||||||
public class JacksonAnnotationUnitTest {
|
public class JacksonAnnotationUnitTest {
|
||||||
|
|
||||||
|
@ -378,13 +380,40 @@ public class JacksonAnnotationUnitTest {
|
||||||
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
|
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
|
||||||
|
|
||||||
// arrange
|
// arrange
|
||||||
String json = "{\"fName\": \"Alex\", \"lastName\": \"Theedom\"}";
|
String json = "{\"fName\": \"John\", \"lastName\": \"Green\"}";
|
||||||
|
|
||||||
// act
|
// act
|
||||||
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
|
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
assertThat(aliasBean.getFirstName(), is("Alex"));
|
assertThat(aliasBean.getFirstName(), is("John"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
|
||||||
|
|
||||||
|
// arrange
|
||||||
|
UserWithRootNamespace author = new UserWithRootNamespace(1, "John");
|
||||||
|
|
||||||
|
// 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("<user xmlns=\"users\">"));
|
||||||
|
|
||||||
|
/*
|
||||||
|
<user xmlns="users">
|
||||||
|
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
|
||||||
|
<id xmlns="">1</id>
|
||||||
|
<name xmlns="">John</name>
|
||||||
|
<items xmlns=""/>
|
||||||
|
</user>
|
||||||
|
*/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue