Modify Json View

This commit is contained in:
DOHA 2014-12-13 18:32:44 +02:00
parent 19a558bfec
commit 4a61383b0c
9 changed files with 73 additions and 42 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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)

View File

@ -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

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
package org.baeldung.web.dto;
public class Views {
public static class Public {
}
public static class Internal extends Public {
}
}