【问题标题】:400 Bad Request Spring Test Web Application400 错误请求 Spring 测试 Web 应用程序
【发布时间】:2013-11-05 15:55:16
【问题描述】:

这是我的控制器:

@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 Bad Request,我不知道为什么...

有人有提示吗? 更新添加类追随者

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

【问题讨论】:

  • 你能发布你的Follower课程吗?
  • 您是否尝试过打印出 asJson 并通过客户端实用程序手动将其发布到您的 REST 端点,以确保模拟框架抽象不会受到干扰?此外,您的端点是否对 Follower 进行任何特殊验证,如果失败则手动抛出 400?
  • 是的,我打印出来了,看起来不错。

标签: java spring testing spring-mvc spring-test


【解决方案1】:

发现问题:

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

我添加了这个方法,jackson 尝试序列化一个名为 ignoreListIterator 的字段...

使用@JsonIgnore 注释可以解决问题。

更新:在这种情况下,最好使用 Java Bean 属性名称约定以外的其他方法,并将方法名称更改为:

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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多