@RequestMapping无法在Spring Boot中运行

时间:2018-04-19 09:53:47

标签: spring spring-boot

控制器类。

@RestController
@RequestMapping("/check")
public class Controller {

    public String index(){
        return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
    }

申请类

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

在运行应用程序时添加了所有必要的依赖项显示

http://localhost:8081/demo/
Hello World 

of index.xml

当我更改为http://localhost:8081/check/时,它会给出

HTTP Status 404 – Not Found
Type Status Report

Message /check

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

如何理解Spring Boot应用程序的流程?

3 个答案:

答案 0 :(得分:0)

你的控制器应该是这样的:

@RestController  
public class Controller {
     @RequestMapping(value="/check")
      public String index(){
          return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
      }
} 

答案 1 :(得分:0)

您需要将Http方法放在您的方法上,我假设您正在执行GET请求

@RestController
@RequestMapping("/check")
public class Controller {
    @GetMapping // you forgot to put http method here
    public String index(){
        return "sdfksdjfkjkUshshdfisdfsdkasjdfjkasjdfkjakl:";
}

注意:GetMapping仅在您使用Spring 4.3或以上时使用@RequestMapping(value = "/url", method = RequestMethod.GET)

答案 2 :(得分:-1)

似乎 @RequestMapping(value =“ / check”)不起作用。

切换到 @RequestMapping(path =“ / check”)

尽管根据文档它应该可以工作。