简体   繁体   中英

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

Assertion error using @RequestMapping annotation outside of the class

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. 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
@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. The first usage is on the type (class) level, which indicates that all handler methods in this controller are relative to the /appointments path.

So the class level @RequestMapping is only indicating relativnes. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM