简体   繁体   English

Springboot Restcontroller拒绝工作

[英]Springboot Restcontroller refusing to work

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. 我花了最后2个小时,试图弄清楚我简单的springboot rest应用程序到底是怎么回事。

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正常工作,我尝试的每个URL都会给我一个404。这是下面的代码。

    @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. 我曾经做过无数次这样的尝试,但是我无法终生弄清为什么它不起作用。

I try to to go to localhost:8088/testMe, and I get a 404. 我尝试去localhost:8088 / testMe,然后得到404。

If PbxPortalApplication and PbxPortalApplication classes are in different package, you need to tell your application to scan the controller while loading up the app context. 如果PbxPortalApplicationPbxPortalApplication类位于不同的程序包中,则需要告诉您的应用程序在加载应用程序上下文时扫描控制器。

Add the @ComponentScan to your PbxPortalApplication class @ComponentScan添加到您的PbxPortalApplication

@SpringBootApplication
@ComponentScan(basePackageClasses = PbxPortalRestControllerSet.class)
public class PbxPortalApplication

I found the issue. 我发现了问题。 I was using the wrong POM entry. 我使用了错误的POM条目。 I was using Jersey somehow instead of the built in spring ones. 我以某种方式使用泽西岛,而不是内置的春天。 For some reason even though I was using the wrong library, eclipse was telling me that my annotation entries were perfectly fine. 由于某种原因,即使我使用了错误的库,eclipse也告诉我我的注释条目完全正确。 Once i deleted the entry for Jersey everything worked 一旦我删除了Jersey的条目,一切正常

@RestController
public class DemoController {

    @GetMapping(value= "/getName")
    public String getName(){
        return "This is a Spring Boot Application";
    }
}

Main Class is simple: 主类很简单:

package com.pcftest.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

Can you try this once ?? 你能尝试一次吗? And see if it works.. 看看是否可行。

@RequestMapping(value= "/testMe" , method = RequestMethod.GET)

Basically sometimes when you dont provide the request method type you get such types of errors 基本上有时候,当您不提供请求方法类型时,会得到此类错误

It seems that you are you have placed your controller different sub package than spring SpringApplication file. 看来您已将控制器放置在与Spring SpringApplication文件不同的子包中。 So Controller is not accessible from Spring main() Please add 所以无法从Spring main()访问Controller请添加

@SpringBootApplication
@ComponentScan("ControllersPackege")

CodeSnipet:SpringBootApplication CodeSnipet:SpringBootApplication

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

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

CodeSnipet:Controller CodeSnipet:控制器

@RestController 
public class PbxPortalRestControllerSet {

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

application.properties file application.properties文件

server.port = 8088

NB: Best way to put in same package or put controller class in sub-package 注意:放入相同程序包或将控制器类放入子程序包的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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