commit
8483143b16
|
@ -9,33 +9,6 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>1.1.9.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -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;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
public class User {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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