![](/img/trans.png)
[英]java.lang.AssertionError: Status expected:<200> but was:<404>
[英]@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404
在类外使用@RequestMapping 注释的断言错误
我收到此错误消息:
java.lang.AssertionError: Status
Expected :200
Actual :404
我的控制器是这样的
@Service
@RestController
@RequestMapping("/execute/files")
@ResponseBody
public class ControllerFiles {
@Autowired
@Qualifier("fileRunner")
ProcessRunnerInterface processRunnerInterfaceFiles;
public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
///code///
}
public List<String>....{
///code///
}
}
我的测试
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ControllerFilesTest {
@Autowired
private MockMvc mockMvc;
@Autowired
ControllerFiles controllerFiles;
@Test
public void testSpringMvcGetFiles() throws Exception {
this.mockMvc.perform(get("/execute/files").param("name", "Spring Community Files"))
.andDo(print()).andExpect(status().isOk());
}
}
但是当我有这样的代码时,测试工作正常!
@Service
@RestController
public class ControllerFiles {
@Autowired
@Qualifier("fileRunner")
ProcessRunnerInterface processRunnerInterfaceFiles;
@RequestMapping("/execute/files")
@ResponseBody
public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
///code///
}
public List<String>....{
///code///
}
}
任何想法出了什么问题?
如果您希望将 RestController 中的方法作为请求资源提取,则需要将它们标记为 @RequestMapping。 如果你想在你的第一个 RestController 中将基本请求映射保留在控制器级别,那么你需要执行以下操作:
@RestController
@RequestMapping("my/path")
public class MyController {
@RequestMapping("/")
public InputState myMethod() {
...
}
}
正如文档中所说:
在上面的例子中,@RequestMapping 用在了很多地方。 第一个用法是在类型(类)级别,它表示此控制器中的所有处理程序方法都与 /appointments 路径相关。
所以类级别@RequestMapping
仅表示相对。 它不是仅基于公共方法声明实际资源路径。 所以你需要像这样注释你的方法:
@GetMapping
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
或者像这样:
@RequestMapping(method = RequestMethod.GET)
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.