Springboot Restcontroller refusing to work

2019-08-19 20:32发布

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.

4条回答
对你真心纯属浪费
2楼-- · 2019-08-19 20:50

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

查看更多
Juvenile、少年°
3楼-- · 2019-08-19 21:01
@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

查看更多
Juvenile、少年°
4楼-- · 2019-08-19 21:05

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

查看更多
唯我独甜
5楼-- · 2019-08-19 21:09

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
查看更多
登录 后发表回答