2013-10-26 83 views
3

這是我的控制器:400錯誤的請求彈簧測試Web應用程序

@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; 

} 

這是我的測試:

@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)); 

} 

但我一直都想與400錯誤的請求,我不能找出原因...

任何人都有提示? 更新添加類跟隨

@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; 
    } 
} 
+0

您可以發佈您'Follower'類? –

+0

您是否試過打印'asJson'並通過客戶端實用程序手動發佈到您的REST端點以確保模擬框架抽象不會干擾?另外,你的端點是否對'Follower'做了任何特殊驗證,如果失敗了,手動拋出400? – Vidya

+0

是的,我打印它,看起來不錯。 –

回答

0

發現了問題:

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

我加入這個方法和傑克遜嘗試序列命名ignoreListIterator場...

與詮釋解決了@JsonIgnore問題。

更新:比Java Bean的屬性命名約定在這種情況下,更好地利用其他並更改方法名稱:

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

感謝