简体   繁体   English

@RequestMapping java.lang.AssertionError:预期状态:200 实际:404

[英]@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.

相关问题 java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> java.lang.AssertionError:预期状态:&lt;200&gt;,但在静态服务中为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> in restful services java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; in Junit test - java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test java.lang.AssertionError:预期状态:&lt;400&gt; 但原为:&lt;200&gt; 预期:400 实际:200 - java.lang.AssertionError: Status expected:<400> but was:<200> Expected :400 Actual :200 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; with test case failed - java.lang.AssertionError: Status expected:<200> but was:<404> with test cases failing java.lang.AssertionError:预期状态:<200> 但为:<404> 对于 Spring 启动 Rest Controller - java.lang.AssertionError: Status expected:<200> but was:<404> for Spring Boot Rest Controller java.lang.AssertionError:预期状态:&lt;200&gt; 但原为:&lt;404&gt; Spring 引导页面未找到 - java.lang.AssertionError: Status expected:<200> but was:<404> Spring Boot Page Not found java.lang.AssertionError: 预期状态:&lt;200&gt; 但为:&lt;400&gt; - java.lang.AssertionError: Status expected:<200> but was:<400> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;201&gt; - java.lang.AssertionError: Status expected:<200> but was:<201> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;405&gt; - java.lang.AssertionError: Status expected:<200> but was:<405>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM