@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404

@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404

在 class

之外使用 @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/// 
               }
}

知道哪里出了问题吗?

正如documentation中所说:

In the above example, @RequestMapping is used in a number of places. The first usage is on the type (class) level, which indicates that all handler methods in this controller are relative to the /appointments path.

所以class水平@RequestMapping只是表示相对性。它不是仅根据 public 方法声明实际资源路径。所以你需要像这样注释你的方法:

@GetMapping
public InputState executeRestFile(@RequestParam String name) throws Exception {
    // omited
}

或者像这样:

@RequestMapping(method = RequestMethod.GET)
public InputState executeRestFile(@RequestParam String name) throws Exception {
    // omited
}

如果您希望将 RestController 中的方法作为请求资源获取,则需要将它们标记为 @RequestMapping。如果你想像在你的第一个 RestController 中一样在控制器级别保留基本请求映射,那么你需要执行以下操作:

@RestController
@RequestMapping("my/path")
public class MyController {

   @RequestMapping("/")
   public InputState myMethod() {
     ...
   }
}