From 4a61383b0cfdeee6cda84eeba332c65c8d0f9f1f Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 13 Dec 2014 18:32:44 +0200 Subject: [PATCH] Modify Json View --- .../jackson/jsonview/Application.java | 14 -------- .../org/baeldung/jackson/jsonview/Item.java | 10 +++--- .../jackson/jsonview/ItemManager.java | 10 ------ .../org/baeldung/jackson/jsonview/User.java | 1 - .../jackson/test/JacksonJsonViewTest.java | 15 ++++---- .../web/controller/ItemController.java | 11 +++--- .../main/java/org/baeldung/web/dto/Item.java | 36 +++++++++++++++++++ .../org/baeldung/web/dto/ItemManager.java | 9 +++++ .../main/java/org/baeldung/web/dto/Views.java | 9 +++++ 9 files changed, 73 insertions(+), 42 deletions(-) delete mode 100644 jackson/src/test/java/org/baeldung/jackson/jsonview/Application.java delete mode 100644 jackson/src/test/java/org/baeldung/jackson/jsonview/ItemManager.java rename jackson/src/test/java/org/baeldung/jackson/jsonview/UserController.java => spring-rest/src/main/java/org/baeldung/web/controller/ItemController.java (67%) create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/Item.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/ItemManager.java create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/Views.java diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/Application.java b/jackson/src/test/java/org/baeldung/jackson/jsonview/Application.java deleted file mode 100644 index eea9745d38..0000000000 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/Application.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.baeldung.jackson.jsonview; - -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.SpringApplication; -import org.springframework.context.annotation.ComponentScan; - -@ComponentScan -@EnableAutoConfiguration -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java b/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java index 758a1338ec..5705f810af 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java +++ b/jackson/src/test/java/org/baeldung/jackson/jsonview/Item.java @@ -10,16 +10,16 @@ public class Item { public String itemName; @JsonView(Views.Internal.class) - public User owner; + public String ownerName; public Item() { super(); } - public Item(final int id, final String itemName, final User owner) { + public Item(final int id, final String itemName, final String ownerName) { this.id = id; this.itemName = itemName; - this.owner = owner; + this.ownerName = ownerName; } public int getId() { @@ -30,7 +30,7 @@ public class Item { return itemName; } - public User getOwner() { - return owner; + public String getOwnerName() { + return ownerName; } } diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/ItemManager.java b/jackson/src/test/java/org/baeldung/jackson/jsonview/ItemManager.java deleted file mode 100644 index 0554a8dba3..0000000000 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/ItemManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.jackson.jsonview; - -public class ItemManager { - - public static Item getById(final int id) { - final User owner = new User(1, "John"); - final Item item = new Item(2, "book", owner); - return item; - } -} diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java b/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java index ed51881cf0..5850dcbe84 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java +++ b/jackson/src/test/java/org/baeldung/jackson/jsonview/User.java @@ -3,7 +3,6 @@ package org.baeldung.jackson.jsonview; import com.fasterxml.jackson.annotation.JsonView; public class User { - @JsonView(Views.Public.class) public int id; @JsonView(Views.Public.class) diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java index 0f0e4272e5..276db9261c 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonJsonViewTest.java @@ -14,6 +14,7 @@ import org.baeldung.jackson.jsonview.Views; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ser.BeanSerializerFactory; import com.fasterxml.jackson.databind.ser.SerializerFactory; @@ -25,16 +26,17 @@ public class JacksonJsonViewTest { final User user = new User(1, "John"); final ObjectMapper mapper = new ObjectMapper(); + mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION); + final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user); assertThat(result, containsString("John")); - assertThat(result, containsString("1")); + assertThat(result, not(containsString("1"))); } @Test public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingException { - final User owner = new User(1, "John"); - final Item item = new Item(2, "book", owner); + final Item item = new Item(2, "book", "John"); final ObjectMapper mapper = new ObjectMapper(); final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(item); @@ -43,13 +45,11 @@ public class JacksonJsonViewTest { assertThat(result, containsString("2")); assertThat(result, not(containsString("John"))); - assertThat(result, not(containsString("1"))); } @Test public void whenUseInternalView_thenAllSerialized() throws JsonProcessingException { - final User owner = new User(1, "John"); - final Item item = new Item(2, "book", owner); + final Item item = new Item(2, "book", "John"); final ObjectMapper mapper = new ObjectMapper(); final String result = mapper.writerWithView(Views.Internal.class).writeValueAsString(item); @@ -58,7 +58,6 @@ public class JacksonJsonViewTest { assertThat(result, containsString("2")); assertThat(result, containsString("John")); - assertThat(result, containsString("1")); } @Test @@ -84,4 +83,4 @@ public class JacksonJsonViewTest { assertThat(result, containsString("JOHN")); assertThat(result, containsString("1")); } -} \ No newline at end of file +} diff --git a/jackson/src/test/java/org/baeldung/jackson/jsonview/UserController.java b/spring-rest/src/main/java/org/baeldung/web/controller/ItemController.java similarity index 67% rename from jackson/src/test/java/org/baeldung/jackson/jsonview/UserController.java rename to spring-rest/src/main/java/org/baeldung/web/controller/ItemController.java index ac487badb1..3c06dd4129 100644 --- a/jackson/src/test/java/org/baeldung/jackson/jsonview/UserController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/ItemController.java @@ -1,5 +1,8 @@ -package org.baeldung.jackson.jsonview; +package org.baeldung.web.controller; +import org.baeldung.web.dto.Item; +import org.baeldung.web.dto.ItemManager; +import org.baeldung.web.dto.Views; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -7,16 +10,16 @@ import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.annotation.JsonView; @RestController -public class UserController { +public class ItemController { @JsonView(Views.Public.class) - @RequestMapping("/{id}") + @RequestMapping("/items/{id}") public Item getItemPublic(@PathVariable final int id) { return ItemManager.getById(id); } @JsonView(Views.Internal.class) - @RequestMapping("/internal/{id}") + @RequestMapping("/items/internal/{id}") public Item getItemInternal(@PathVariable final int id) { return ItemManager.getById(id); } diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/Item.java b/spring-rest/src/main/java/org/baeldung/web/dto/Item.java new file mode 100644 index 0000000000..536c72020f --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/Item.java @@ -0,0 +1,36 @@ +package org.baeldung.web.dto; + +import com.fasterxml.jackson.annotation.JsonView; + +public class Item { + @JsonView(Views.Public.class) + public int id; + + @JsonView(Views.Public.class) + public String itemName; + + @JsonView(Views.Internal.class) + public String ownerName; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final String ownerName) { + this.id = id; + this.itemName = itemName; + this.ownerName = ownerName; + } + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public String getOwnerName() { + return ownerName; + } +} \ No newline at end of file diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/ItemManager.java b/spring-rest/src/main/java/org/baeldung/web/dto/ItemManager.java new file mode 100644 index 0000000000..74ffada300 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/ItemManager.java @@ -0,0 +1,9 @@ +package org.baeldung.web.dto; + +public class ItemManager { + + public static Item getById(final int id) { + final Item item = new Item(2, "book", "John"); + return item; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/Views.java b/spring-rest/src/main/java/org/baeldung/web/dto/Views.java new file mode 100644 index 0000000000..6231e12bcc --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/Views.java @@ -0,0 +1,9 @@ +package org.baeldung.web.dto; + +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +}