commit
e667993c18
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.jackson.bidirection;
|
||||
|
||||
import org.baeldung.jackson.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class ItemWithView {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String itemName;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public UserWithView owner;
|
||||
|
||||
public ItemWithView() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ItemWithView(final int id, final String itemName, final UserWithView owner) {
|
||||
this.id = id;
|
||||
this.itemName = itemName;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package org.baeldung.jackson.bidirection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.jackson.jsonview.Views;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class UserWithView {
|
||||
@JsonView(Views.Public.class)
|
||||
public int id;
|
||||
|
||||
@JsonView(Views.Public.class)
|
||||
public String name;
|
||||
|
||||
@JsonView(Views.Internal.class)
|
||||
public List<ItemWithView> userItems;
|
||||
|
||||
public UserWithView() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserWithView(final int id, final String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
userItems = new ArrayList<ItemWithView>();
|
||||
}
|
||||
|
||||
public void addItem(final ItemWithView item) {
|
||||
userItems.add(item);
|
||||
}
|
||||
}
|
|
@ -12,11 +12,14 @@ import org.baeldung.jackson.bidirection.ItemWithIdentity;
|
|||
import org.baeldung.jackson.bidirection.ItemWithIgnore;
|
||||
import org.baeldung.jackson.bidirection.ItemWithRef;
|
||||
import org.baeldung.jackson.bidirection.ItemWithSerializer;
|
||||
import org.baeldung.jackson.bidirection.ItemWithView;
|
||||
import org.baeldung.jackson.bidirection.User;
|
||||
import org.baeldung.jackson.bidirection.UserWithIdentity;
|
||||
import org.baeldung.jackson.bidirection.UserWithIgnore;
|
||||
import org.baeldung.jackson.bidirection.UserWithRef;
|
||||
import org.baeldung.jackson.bidirection.UserWithSerializer;
|
||||
import org.baeldung.jackson.bidirection.UserWithView;
|
||||
import org.baeldung.jackson.jsonview.Views;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
@ -107,4 +110,26 @@ public class JacksonBidirectionRelationTest {
|
|||
assertEquals("John", item.owner.name);
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void givenBidirectionRelation_whenUsingPublicJsonView_thenCorrect() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
final String result = new ObjectMapper().writerWithView(Views.Public.class).writeValueAsString(item);
|
||||
|
||||
assertThat(result, containsString("book"));
|
||||
assertThat(result, containsString("John"));
|
||||
assertThat(result, not(containsString("userItems")));
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void givenBidirectionRelation_whenUsingInternalJsonView_thenException() throws JsonProcessingException {
|
||||
final UserWithView user = new UserWithView(1, "John");
|
||||
final ItemWithView item = new ItemWithView(2, "book", user);
|
||||
user.addItem(item);
|
||||
|
||||
new ObjectMapper().writerWithView(Views.Internal.class).writeValueAsString(item);
|
||||
}
|
||||
|
||||
}
|
|
@ -15,12 +15,16 @@ import org.baeldung.persistence.model.User;
|
|||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
|
||||
@Controller
|
||||
|
@ -86,28 +90,18 @@ public class UserController {
|
|||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/new")
|
||||
@ResponseBody
|
||||
public long addUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) {
|
||||
final User user = new User();
|
||||
user.setFirstName(first);
|
||||
user.setLastName(last);
|
||||
user.setEmail("john@doe.com");
|
||||
user.setAge(age);
|
||||
dao.save(user);
|
||||
return user.getId();
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody final User resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
dao.save(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers/new")
|
||||
@ResponseBody
|
||||
public long addMyUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) {
|
||||
final MyUser user = new MyUser();
|
||||
user.setFirstName(first);
|
||||
user.setLastName(last);
|
||||
user.setEmail("john@doe.com");
|
||||
user.setAge(age);
|
||||
mydao.save(user);
|
||||
return user.getId();
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void addMyUser(@RequestBody final MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
mydao.save(resource);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue