Modify Json View
This commit is contained in:
parent
19a558bfec
commit
4a61383b0c
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,16 +10,16 @@ public class Item {
|
||||||
public String itemName;
|
public String itemName;
|
||||||
|
|
||||||
@JsonView(Views.Internal.class)
|
@JsonView(Views.Internal.class)
|
||||||
public User owner;
|
public String ownerName;
|
||||||
|
|
||||||
public Item() {
|
public Item() {
|
||||||
super();
|
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.id = id;
|
||||||
this.itemName = itemName;
|
this.itemName = itemName;
|
||||||
this.owner = owner;
|
this.ownerName = ownerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -30,7 +30,7 @@ public class Item {
|
||||||
return itemName;
|
return itemName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getOwner() {
|
public String getOwnerName() {
|
||||||
return owner;
|
return ownerName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,7 +3,6 @@ package org.baeldung.jackson.jsonview;
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
@JsonView(Views.Public.class)
|
|
||||||
public int id;
|
public int id;
|
||||||
|
|
||||||
@JsonView(Views.Public.class)
|
@JsonView(Views.Public.class)
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.baeldung.jackson.jsonview.Views;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
|
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
|
||||||
import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
import com.fasterxml.jackson.databind.ser.SerializerFactory;
|
||||||
|
@ -25,16 +26,17 @@ public class JacksonJsonViewTest {
|
||||||
final User user = new User(1, "John");
|
final User user = new User(1, "John");
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
|
||||||
|
|
||||||
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
|
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
|
||||||
|
|
||||||
assertThat(result, containsString("John"));
|
assertThat(result, containsString("John"));
|
||||||
assertThat(result, containsString("1"));
|
assertThat(result, not(containsString("1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingException {
|
public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingException {
|
||||||
final User owner = new User(1, "John");
|
final Item item = new Item(2, "book", "John");
|
||||||
final Item item = new Item(2, "book", owner);
|
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(item);
|
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(item);
|
||||||
|
@ -43,13 +45,11 @@ public class JacksonJsonViewTest {
|
||||||
assertThat(result, containsString("2"));
|
assertThat(result, containsString("2"));
|
||||||
|
|
||||||
assertThat(result, not(containsString("John")));
|
assertThat(result, not(containsString("John")));
|
||||||
assertThat(result, not(containsString("1")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseInternalView_thenAllSerialized() throws JsonProcessingException {
|
public void whenUseInternalView_thenAllSerialized() throws JsonProcessingException {
|
||||||
final User owner = new User(1, "John");
|
final Item item = new Item(2, "book", "John");
|
||||||
final Item item = new Item(2, "book", owner);
|
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String result = mapper.writerWithView(Views.Internal.class).writeValueAsString(item);
|
final String result = mapper.writerWithView(Views.Internal.class).writeValueAsString(item);
|
||||||
|
@ -58,7 +58,6 @@ public class JacksonJsonViewTest {
|
||||||
assertThat(result, containsString("2"));
|
assertThat(result, containsString("2"));
|
||||||
|
|
||||||
assertThat(result, containsString("John"));
|
assertThat(result, containsString("John"));
|
||||||
assertThat(result, containsString("1"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -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.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -7,16 +10,16 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class UserController {
|
public class ItemController {
|
||||||
|
|
||||||
@JsonView(Views.Public.class)
|
@JsonView(Views.Public.class)
|
||||||
@RequestMapping("/{id}")
|
@RequestMapping("/items/{id}")
|
||||||
public Item getItemPublic(@PathVariable final int id) {
|
public Item getItemPublic(@PathVariable final int id) {
|
||||||
return ItemManager.getById(id);
|
return ItemManager.getById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonView(Views.Internal.class)
|
@JsonView(Views.Internal.class)
|
||||||
@RequestMapping("/internal/{id}")
|
@RequestMapping("/items/internal/{id}")
|
||||||
public Item getItemInternal(@PathVariable final int id) {
|
public Item getItemInternal(@PathVariable final int id) {
|
||||||
return ItemManager.getById(id);
|
return ItemManager.getById(id);
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
public class Views {
|
||||||
|
public static class Public {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Internal extends Public {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue