java - 400 错误请求 Spring 测试 Web 应用程序

标签 java spring testing spring-mvc spring-test

这是我的 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;

}

这是我的测试:

@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 并且我无法弄清楚为什么...

谁有提示? 更新 添加类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;
    }
}

最佳答案

发现问题:

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

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

用@JsonIgnore 注释解决了这个问题。

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

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

谢谢

关于java - 400 错误请求 Spring 测试 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19608335/

相关文章:

java - Nodejs 加密 ECDH 公钥十六进制为 X.509

c# - 如何使任务失败然后断言异常

java - 是否有类似于 usingColumns() 的 SimpleJdbcInsert 方法,该方法将排除的列列表作为参数?

java - 如何在 Wicket Session 类中 Autowiring Spring bean?

iphone - 使用 Telerik TestStudio 和 Jenkins 的连续 iPhone 集成测试服务器

linux - 有没有办法使用 Jenkins 运行交互式测试脚本(并让用户实际与之交互)?

java - 更改 Java 动态列表或映射中的对象引用

java - 如何验证 Json 属性(键)名称?

java - 如何使用 Selenium 为 Firefox 和 Chrome 禁用推送通知?

eclipse - Petclinic spring-mvc 示例未构建