简体   繁体   中英

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.

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.

I try to to go to localhost:8088/testMe, and I get a 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.

Add the @ComponentScan to your PbxPortalApplication class

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

I found the issue. I was using the wrong POM entry. 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. Once i deleted the entry for Jersey everything worked

@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. 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:Controller

@RestController 
public class PbxPortalRestControllerSet {

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

application.properties file

server.port = 8088

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

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