4

This is my controller:

@RequestMapping(value = "/followers", method = RequestMethod.POST, consumes = "application/json" , produces = "application/json") @ResponseBody public Follower create(@RequestBody Follower follower, HttpServletResponse response) { Follower savedFollower = service.create(follower); response.setStatus(201); //Created return savedfollower; } 

And this is my test:

@Test public void post_should_create_new_follower() throws Exception { Follower follower = new FollowerDummyBuilder().build(); ObjectMapper objectMapper = new ObjectMapper(); String asJson = objectMapper.writeValueAsString(follower); assertEquals(0l, JPATestUtils.countEntity(em, Follower.class)); this.mockMvc.perform(MockMvcRequestBuilders.post("/followers"). accept(MediaType.APPLICATION_JSON). contentType(MediaType.APPLICATION_JSON).content(asJson)).andExpect((MockMvcResultMatchers.status().isCreated())); assertEquals(1l, JPATestUtils.countEntity(em, Follower.class)); } 

But I'm always getting 400 Bad Request and I'm not able to figure out why...

Anyone have a hint? Update Adding class Follower

@Entity public class Follower extends AbstractEntity { @OneToOne(cascade = CascadeType.ALL) @NotNull private Credentials Credentials; @NotNull @OneToOne(cascade = CascadeType.ALL) private UserRetrievalStrategy userRetrievalStrategy; @NotNull @OneToOne(cascade = CascadeType.ALL) private SearchEngineFilter searchEngineFilter; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JsonIgnore private IgnoreList ignoreList = new IgnoreList(); @Enumerated(EnumType.STRING) @NotNull private FollowerStatus status; protected Follower() { } public Follower(Credentials credentials, UserRetrievalStrategy userRetrievalStrategy, SearchEngineFilter searchEngineFilter) { this.credentials = credentials; this.userRetrievalStrategy = userRetrievalStrategy; this.searchEngineFilter = searchEngineFilter; this.status = FollowerStatus.STOPED; } public Credentials getCredentials() { return credentials; } public void addUserToIgnoreList(User user) { this.ignoreList.add(user); } public IgnoreList getIgnoreList() { return ignoreList; } public UserRetrievalStrategy getUserRetrievalStrategy() { return userRetrievalStrategy; } public SearchEngineFilter getSearchEngineFilter() { return searchEngineFilter; } public void setSearchEngineFilter(SearchEngineFilter searchEngineFilter) { this.searchEngineFilter = searchEngineFilter; } public Iterator<User> getIgnoreListIterator() { return ignoreList.iterator(); } public void clearIgnoreList() { this.ignoreList.clearList(); this.ignoreList = new IgnoreList(); } public void setStaus(AutofollowerStatus status) { this.status = status; } public AutofollowerStatus getStatus() { return status; } } 
3
  • Can you post your Follower class? Commented Oct 26, 2013 at 15:21
  • Have you tried printing out asJson and posting it manually to your REST endpoint via a client utility just to make sure the mock framework abstraction isn't interfering? Also, does your endpoint do any special validation on Follower and manually throw a 400 if it fails? Commented Oct 26, 2013 at 15:43
  • Yes, I print it, and looks good. Commented Oct 26, 2013 at 15:49

1 Answer 1

0

Found the problem:

public Iterator<User> getIgnoreListIterator() { return ignoreList.iterator(); } 

I added this method and jackson tries to serialize a field named ignoreListIterator...

Annotating with @JsonIgnore solves the problem.

Update: Better in this case use other than the Java Bean property name convention and change the method name to:

public Iterator<User> ignoreListIterator() { return ignoreList.iterator(); } 

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.