I have two files in my application.
The Main class file.
package in.njari.util;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "in.njari")
public class UtilApplication {
public static void main(String[] args) {
SpringApplication.run(UtilApplication.class, args);
}
}
The controller class.
package in.njari.util.src.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UtilController {
@GetMapping(value = "/util")
public String f(){
return "I provide insights.";
}
}
My application.properties looks like :
server.servlet.context-path=/util-service
logging.level.org.springframework.core.io.support=DEBUG
logging.level.org.springframework.context.annotation=DEBUG
Here's what my console looks like :
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2022-04-06 11:19:53.819 INFO 57700 --- [ main] in.njari.util.UtilApplication : Starting UtilApplication on njari-MacBook-Pro.local with PID 57700 (/Users/rajdeep/X/util/target/classes started by rajdeep in /Users/rajdeep/X/util)
2022-04-06 11:19:53.821 INFO 57700 --- [ main] in.njari.util.UtilApplication : No active profile set, falling back to default profiles: default
2022-04-06 11:19:54.005 DEBUG 57700 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/Users/njari/X/util/target/classes/in/njari/util/src/controller/UtilController.class]
2022-04-06 11:19:54.615 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-06 11:19:54.627 INFO 57700 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-06 11:19:54.627 INFO 57700 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-04-06 11:19:54.708 INFO 57700 --- [ main] o.a.c.c.C.[.[localhost].[/util-service] : Initializing Spring embedded WebApplicationContext
2022-04-06 11:19:54.709 INFO 57700 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 830 ms
2022-04-06 11:19:54.861 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/util-service'
2022-04-06 11:19:54.866 INFO 57700 --- [ main] in.njari.util.UtilApplication : Started UtilApplication in 6.463 seconds (JVM running for 6.993)
I tried hitting the api with the url :
http://localhost:8080/util-service/util
and this throws up a 404 error.
So even though the controller is picked up for component scanning(according to the logs), the request within it isn't mapped. Not sure why this is happening.
I suspect there is some wrong configuration/annotation that i'm using/not using here! Haven't been able to pinpoint what it really is though.
CodePudding user response:
try removing
@ComponentScan(basePackages = "in.njari")
the annotation @RestController
is a java bean that the system will recognize and initialize by its own, Other then that your code seems to work I have tested it.
CodePudding user response:
From 2.1.x of Spring Boot the "spring.mvc.servlet.path" property is moved and renamed as "spring.mvc.servlet.path".
Can you try
spring.mvc.servlet.path=/util-service
instead of
server.servlet.context-path=/util-service
CodePudding user response:
If you explicitly want to scan base packages, use
@SpringBootApplication(scanBasePackages = "in.njari")