Add Jackson JSON View Test

This commit is contained in:
DOHA 2014-12-11 21:29:11 +02:00
parent 01fc47a83d
commit 19a558bfec
10 changed files with 277 additions and 0 deletions

View File

@ -9,6 +9,33 @@
<dependencies> <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 --> <!-- utils -->
<dependency> <dependency>

View File

@ -0,0 +1,14 @@
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

@ -0,0 +1,36 @@
package org.baeldung.jackson.jsonview;
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 User owner;
public Item() {
super();
}
public Item(final int id, final String itemName, final User owner) {
this.id = id;
this.itemName = itemName;
this.owner = owner;
}
public int getId() {
return id;
}
public String getItemName() {
return itemName;
}
public User getOwner() {
return owner;
}
}

View File

@ -0,0 +1,10 @@
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

@ -0,0 +1,22 @@
package org.baeldung.jackson.jsonview;
import java.util.List;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
public class MyBeanSerializerModifier extends BeanSerializerModifier {
@Override
public List<BeanPropertyWriter> changeProperties(final SerializationConfig config, final BeanDescription beanDesc, final List<BeanPropertyWriter> beanProperties) {
for (int i = 0; i < beanProperties.size(); i++) {
final BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
if (beanPropertyWriter.getName() == "name") {
beanProperties.set(i, new UpperCasingWriter(beanPropertyWriter));
}
}
return beanProperties;
}
}

View File

@ -0,0 +1,21 @@
package org.baeldung.jackson.jsonview;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
public class UpperCasingWriter extends BeanPropertyWriter {
final BeanPropertyWriter _writer;
public UpperCasingWriter(final BeanPropertyWriter w) {
super(w);
_writer = w;
}
@Override
public void serializeAsField(final Object bean, final JsonGenerator gen, final SerializerProvider prov) throws Exception {
String value = ((User) bean).name;
value = (value == null) ? "" : value.toUpperCase();
gen.writeStringField("name", value);
}
}

View File

@ -0,0 +1,28 @@
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)
public String name;
public User() {
super();
}
public User(final int id, final String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,23 @@
package org.baeldung.jackson.jsonview;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
public class UserController {
@JsonView(Views.Public.class)
@RequestMapping("/{id}")
public Item getItemPublic(@PathVariable final int id) {
return ItemManager.getById(id);
}
@JsonView(Views.Internal.class)
@RequestMapping("/internal/{id}")
public Item getItemInternal(@PathVariable final int id) {
return ItemManager.getById(id);
}
}

View File

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

View File

@ -0,0 +1,87 @@
package org.baeldung.jackson.test;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.baeldung.jackson.jsonview.Item;
import org.baeldung.jackson.jsonview.MyBeanSerializerModifier;
import org.baeldung.jackson.jsonview.User;
import org.baeldung.jackson.jsonview.Views;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
public class JacksonJsonViewTest {
@Test
public void whenUseJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
final User user = new User(1, "John");
final ObjectMapper mapper = new ObjectMapper();
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
assertThat(result, containsString("John"));
assertThat(result, 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 ObjectMapper mapper = new ObjectMapper();
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(item);
assertThat(result, containsString("book"));
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 ObjectMapper mapper = new ObjectMapper();
final String result = mapper.writerWithView(Views.Internal.class).writeValueAsString(item);
assertThat(result, containsString("book"));
assertThat(result, containsString("2"));
assertThat(result, containsString("John"));
assertThat(result, containsString("1"));
}
@Test
public void whenUseJsonViewToDeserialize_thenCorrect() throws IOException {
final String json = "{\"id\":1,\"name\":\"John\"}";
final ObjectMapper mapper = new ObjectMapper();
final User user = mapper.readerWithView(Views.Public.class).withType(User.class).readValue(json);
assertEquals(1, user.getId());
assertEquals("John", user.getName());
}
@Test
public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessingException {
final User user = new User(1, "John");
final SerializerFactory serializerFactory = BeanSerializerFactory.instance.withSerializerModifier(new MyBeanSerializerModifier());
final ObjectMapper mapper = new ObjectMapper();
mapper.setSerializerFactory(serializerFactory);
final String result = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
assertThat(result, containsString("JOHN"));
assertThat(result, containsString("1"));
}
}