[英]@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404
Assertion error using @RequestMapping annotation outside of the class在类外使用@RequestMapping 注释的断言错误
I am getting this error message:我收到此错误消息:
java.lang.AssertionError: Status
Expected :200
Actual :404
My Controller is like this我的控制器是这样的
@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///
}
}
My Test我的测试
@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());
}
}
But when I have my code like this the test work fine!但是当我有这样的代码时,测试工作正常!
@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///
}
}
Any ideas what is going wrong?任何想法出了什么问题?
The methods in your RestController need to be marked as @RequestMapping if you want them to be picked up as request resources.如果您希望将 RestController 中的方法作为请求资源提取,则需要将它们标记为 @RequestMapping。 If you want to keep the base request mapping at the controller level as in your first RestController then you need to do the following:如果你想在你的第一个 RestController 中将基本请求映射保留在控制器级别,那么你需要执行以下操作:
@RestController
@RequestMapping("my/path")
public class MyController {
@RequestMapping("/")
public InputState myMethod() {
...
}
}
As it is said in documentation :正如文档中所说:
In the above example, @RequestMapping is used in a number of places.在上面的例子中,@RequestMapping 用在了很多地方。 The first usage is on the type (class) level, which indicates that all handler methods in this controller are relative to the /appointments path.第一个用法是在类型(类)级别,它表示此控制器中的所有处理程序方法都与 /appointments 路径相关。
So the class level @RequestMapping
is only indicating relativnes.所以类级别@RequestMapping
仅表示相对。 It is not declare actual resource paths based on public methods only.它不是仅基于公共方法声明实际资源路径。 So you need to annotate your method like this:所以你需要像这样注释你的方法:
@GetMapping
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
Or like this:或者像这样:
@RequestMapping(method = RequestMethod.GET)
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.