Skip to content

Commit 53f4bbf

Browse files
authored
Merge pull request eugenp#6503 from eugenp/BAEL-12185-v2
new example for jackson article
2 parents 087e321 + 7f24cba commit 53f4bbf

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.jackson.exception;
2+
3+
import com.fasterxml.jackson.annotation.JsonRootName;
4+
5+
@JsonRootName(value = "user", namespace="users")
6+
public class UserWithRootNamespace {
7+
public int id;
8+
public String name;
9+
10+
public UserWithRootNamespace() {
11+
super();
12+
}
13+
14+
public UserWithRootNamespace(final int id, final String name) {
15+
this.id = id;
16+
this.name = name;
17+
}
18+
}

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonAnnotationUnitTest.java‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
3838
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
3939
import com.baeldung.jackson.exception.UserWithRoot;
40+
import com.baeldung.jackson.exception.UserWithRootNamespace;
4041
import com.baeldung.jackson.jsonview.Item;
4142
import com.baeldung.jackson.jsonview.Views;
4243
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -47,6 +48,7 @@
4748
import com.fasterxml.jackson.databind.ser.FilterProvider;
4849
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
4950
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
51+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
5052

5153
public class JacksonAnnotationUnitTest {
5254

@@ -378,13 +380,40 @@ public void whenDisablingAllAnnotations_thenAllDisabled() throws JsonProcessingE
378380
public void whenDeserializingUsingJsonAlias_thenCorrect() throws IOException {
379381

380382
// arrange
381-
String json = "{\"fName\": \"Alex\", \"lastName\": \"Theedom\"}";
383+
String json = "{\"fName\": \"John\", \"lastName\": \"Green\"}";
382384

383385
// act
384386
AliasBean aliasBean = new ObjectMapper().readerFor(AliasBean.class).readValue(json);
385387

386388
// assert
387-
assertThat(aliasBean.getFirstName(), is("Alex"));
389+
assertThat(aliasBean.getFirstName(), is("John"));
388390
}
391+
392+
@Test
393+
public void whenSerializingUsingXMLRootNameWithNameSpace_thenCorrect() throws JsonProcessingException {
394+
395+
// arrange
396+
UserWithRootNamespace author = new UserWithRootNamespace(1, "John");
397+
398+
// act
399+
ObjectMapper mapper = new XmlMapper();
400+
mapper = mapper.enable(SerializationFeature.WRAP_ROOT_VALUE).enable(SerializationFeature.INDENT_OUTPUT);
401+
String result = mapper.writeValueAsString(author);
402+
403+
// assert
404+
assertThat(result, containsString("<user xmlns=\"users\">"));
405+
406+
/*
407+
<user xmlns="users">
408+
<id xmlns="">3006b44a-cf62-4cfe-b3d8-30dc6c46ea96</id>
409+
<id xmlns="">1</id>
410+
<name xmlns="">John</name>
411+
<items xmlns=""/>
412+
</user>
413+
*/
414+
415+
}
416+
417+
389418

390419
}

0 commit comments

Comments
 (0)