0

I'm seeting up Rest end point & @Restcontroller doesnt accept/Map any incomming requests if placed in difefrent package,but works fine if placed in main class package

Spring boot being set-up in Eclipse IDE.

SpringApp.java (main method) in com.java package      --------- Work's fine


@SpringBootApplication
@RestController
public class SpringApp
{


       @RequestMapping("/welcome")
    String welcome() {
        return "Hello!! welcome";
    }


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


    }

}



TestController.java in com.controller package    ----  Doesn't work

@RestController
public class TestController {


    @RequestMapping("/welcome")
    String welcome() {
        return "Hello!! welcome";
    }


I expected the request to get mapped irrespective of the package in which they are placed

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Shawn Saldanha
  • 63
  • 1
  • 1
  • 8
  • Does this answer your question? [@RestController in other package doesn't work](https://stackoverflow.com/questions/33039774/restcontroller-in-other-package-doesnt-work) – Sid Feb 27 '22 at 11:12

3 Answers3

1

you have to specify packages to be scanned while starting application @ComponentScan(basePackages = { "com.controller"})

1

place it in a package under the package where the main class is

in your case the package name is com.java.controller

0

Duplicate with @RestController in other package doesn't work

Vladutz
  • 75
  • 1
  • 9