我在Spring(2.0.5)中具有以下控制器
@RestController
public class FaqController {
@GetMapping("/faqs")
public void get(@RequestParam("language") final Locale locale) {
System.out.println(locale);
}
}
当我使用网址http://localhost:8080/faqs?language
通过Postman / cURL执行GET请求时,将调用控制器方法,而locale
为null
。
当我使用@MvcTest注释的JUnit测试执行相同的URL时,如下所示:
@RunWith(SpringRunner.class)
@WebMvcTest
public class FaqControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
this.mockMvc.perform(get("/faqs?language"));
}
}
然后不调用该方法,而是引发org.springframework.web.bind.MissingServletRequestParameterException
,并显示以下消息:不存在必需的区域设置参数“ language” 。
为什么有区别?我想念什么吗?
答案 0 :(得分:0)
您要它为空吗?如果不需要,则将其删除或将其设置为不需要。
@RestController
public class FaqController {
@GetMapping("/faqs")
public void get(@RequestParam( value = "language", required = false ) final Locale locale) {
System.out.println(locale);
}
}