gpt4 book ai didi

java - 常量 MissingServletRequestParameterException

转载 作者:行者123 更新时间:2023-11-28 21:22:40 24 4
gpt4 key购买 nike

出于某种原因,我不断收到错误消息:

MissingServletRequestParameterException: Required String[] parameter 'ids' is not present

我有一个可以从 /products?ids=["item1", "item2"]

调用的端点

我的集成测试类如下所示:

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(MockitoJUnitRunner.class)
public class ProductControllerTest {

private MockMvc mockMvc;

@InjectMocks
private ProductController controller;

@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}

@Test
public void shouldFetchProducts() throws Exception {
mockMvc.perform(get("/products")
.param("ids[]", "[\"item\"]"))
.andExpect(status().is2xxSuccessful());
}
}

我尝试过的:

  1. 使用参数

    @Test
    public void shouldFetchProducts() throws Exception {
    mockMvc.perform(get("/products")
    .param("ids[]", "[\"item\"]"))
    .andExpect(status().is2xxSuccessful());
    }
  2. 使用requestAttr

    @Test
    public void shouldFetchProducts() throws Exception {
    mockMvc.perform(get("/products")
    .requestAttr("ids[]", "[\"item\"]"))
    .andExpect(status().is2xxSuccessful());
    }

我做错了什么?

最佳答案

你试过吗

@Test
public void shouldFetchProducts() throws Exception {
mockMvc.perform(get("/products")
.param("ids", "item1", "item2"))
.andExpect(status().is2xxSuccessful());
}

可以找到param 的文档here .

关于java - 常量 MissingServletRequestParameterException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com