njari
njari

Reputation: 192

Spring Boot - Controller is recognised but the api is not binded

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;

@SpringBootApplication
public class UtilApplication {

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

}

The controller class.

package in.njari.util.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UtilController {
    @GetMapping("/util")
    public ResponseEntity<?> f(){
        return new ResponseEntity<>("I provide insights.", HttpStatus.OK);
    }
}

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 something wrong with the way i'm configuring my request here! Haven't been able to pinpoint what it really is though.

Upvotes: 0

Views: 207

Answers (2)

Chetan Ahirrao
Chetan Ahirrao

Reputation: 1602

If you explicitly want to scan base packages, use

@SpringBootApplication(scanBasePackages = "in.njari")

Edit: Updating answer w.r.t. your code
Change spring-web dependency to

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Upvotes: 1

Tamir Hazut
Tamir Hazut

Reputation: 36

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.

enter image description here

Upvotes: 0

Related Questions