Am new to spring boot unable to get the issue resolved. In below Screenshots i have commented "/hello" which i could access with localhost:1200/hello .But unable to access other controllers as "/welcome" ,"/home" attached screenshots .
Asked
Active
Viewed 424 times
0
-
1Do you get `404` when you try to access the urls you want ? What do you expect to happen when you access the /welcome method ? It is strange because you are annotating the class with `@RestController` but you return `String` and you add some `Object` to a `Model` which is most likely behaviour of MVC ? – Lazar Lazarov Jun 08 '17 at 07:48
-
Possible duplicate of [Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI \[...\] in DispatcherServlet"?](https://stackoverflow.com/questions/41577234/why-does-spring-mvc-respond-with-a-404-and-report-no-mapping-found-for-http-req) – Sotirios Delimanolis Jun 12 '17 at 19:29
-
@SotiriosDelimanolis Both the questn are different & resolution too how come it becomes duplicate? – Ansuman Jun 13 '17 at 13:25
1 Answers
0
Since your @SpringBootApplication
annotation is in com.example.demo
, and no basePackage is defined for scanning, it will only scan that package and its nested packages. The easiest way to solve this is to move your controller package.
From this:
.
└── main
├── java
│ └── com
│ └── example
│ ├── controller
│ │ └── DemoRestController.java
│ └── demo
│ └── DemoApplication.java
└── resources
└── application.properties
To this:
.
└── main
├── java
│ └── com
│ └── example
│ └── demo
│ ├── controller
│ │ └── DemoRestController.java
│ └── DemoApplication.java
└── resources
└── application.properties
Doc from @ComponentScan
:
Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

Pär Nilsson
- 2,259
- 15
- 19
-
Thanx for reply but i don't think that is the issue pls find below am trying to call localhost:1200/home that too unable to access .@RestController public class HelloWorld { @RequestMapping("/home") String home() { return "Hello World!"; } } – Ansuman Jun 08 '17 at 07:13
-
Thanx nillsson but i tried the above too .But not able to access . I have changed port no. to 1200 in application.properties file .One thing am noticing if am putting a @requestmapping inside Spring application Class am could able to access but not in any other separate Class RestController SpringBootApplication public class MyFirstDemoPrjctApplication extends SpringBootServletInitializer{ RequestMapping("/hello") String home() { return "Hello !"; } public static void main(String[] args) { SpringApplication.run(MyFirstDemoPrjctApplication.class, args); } } – Ansuman Jun 08 '17 at 08:38
-
Above code works fine if am putting all in one file but if a creating another file with class DemoRestController, not able to access from their . – Ansuman Jun 08 '17 at 08:42
-
I think @SpringBootApplication is not able scan all annotation throughout project Is it possible? Please suggest if i need to do any configuration level changes .Appreciate your help – Ansuman Jun 08 '17 at 09:21
-
`@SpringBootApplication` includes `@ComponentScan` which by default scans from the package where your `@SpringBootApplication` annotated class resides. So if you have your controller class in the same package or any nested package it will work. – Pär Nilsson Jun 08 '17 at 11:12
-
Thank you @nilsson its worked it was due to class visibility problem . – Ansuman Jun 12 '17 at 04:59
-
Glad it worked out, consider upvoting if my answer was useful @Ansuman – Pär Nilsson Jun 12 '17 at 05:08