Springboot Restcontroller 拒绝工作 [英] Springboot Restcontroller refusing to work

查看:55
本文介绍了Springboot Restcontroller 拒绝工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的 2 个小时里,我一直试图弄清楚我的简单 springboot rest 应用程序到底发生了什么.

I have spent the last 2 hours trying to figure out just what in gods name is going on with my simple springboot rest app.

无论我做什么,我都无法让 restcontroller 工作,我尝试的每个 URL 都会给我 404.这是我的代码如下.

NO matter what I do, I simply cannot get the restcontroller to work, every URL I try gives me a 404. Here is my code below.

    @RestController 
    public class PbxPortalRestControllerSet {

     @RequestMapping("/testMe")
     public String testMe()
     {
      return "I am alive";
     }
   }



    @SpringBootApplication
    public class PbxPortalApplication {

    public static void main(String[] args) {
    SpringApplication.run(PbxPortalApplication.class, args);
     }
}
 Application.properties file
 server.port = 8088

谁能告诉我这是怎么回事?我之前已经做过很多次了,但我终其一生都无法弄清楚为什么这拒绝工作.

can anyone tell what the heck is going on? I have done this tons of times before, but I can't for the life of me figure out why this refuses to work.

我尝试访问 localhost:8088/testMe,但得到 404.

I try to to go to localhost:8088/testMe, and I get a 404.

推荐答案

看来您放置的控制器与 spring SpringApplication 文件不同的子包.所以控制器不能从 Spring main() 访问请添加

It seems that you are you have placed your controller different sub package than spring SpringApplication file. So Controller is not accessible from Spring main() Please add

@SpringBootApplication
@ComponentScan("ControllersPackege")

CodeSnipet:SpringBootApplication

@SpringBootApplication
@ComponentScan("ControllerPackege")
public class PbxPortalApplication {

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

CodeSnipet:控制器

@RestController 
public class PbxPortalRestControllerSet {

@RequestMapping("/testMe")
     public String testMe()
     {
      return "I am alive";
     }
}

application.properties 文件

server.port = 8088

注意:放在同一个包中或将控制器类放在子包中的最佳方法

NB: Best way to put in same package or put controller class in sub-package

这篇关于Springboot Restcontroller 拒绝工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆