I have written a Spring Boot application with RestContoller. But when I hit the URL in a browser, I get a 404 error. But i am not sure what I am doing wrong. Everything seems to be all correct. My controller class looks like this:
package com.gtt.wcas.device.root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DeviceConnectionController {
Logger logger = LoggerFactory.getLogger(DeviceConnectionController.class.getName());
.
.
.
// Lots of variables
.
.
.
@RequestMapping(value = "/deviceConnection", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> deviceConnection(@RequestParam(value = "verify", defaultValue = "false") String verify) {
.
.
//Method body.... lots of code...
.
.
return new ResponseEntity<String>(HttpStatus.OK);
}
@RequestMapping(value = "/error", method = RequestMethod.GET)
public ResponseEntity<String> error () {
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
.
.
.
// Lots of other helper methods.
.
.
.
}
My main class is like this:
package com.gtt.wcas.device.root;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.gtt.wcas.device.details")
@ConfigurationPropertiesScan(basePackages = "com.gtt.wcas.device.configuration")
@EntityScan(basePackages = "com.gtt.wcas.device.db.jpa")
public class wcasDeviceConnectionApplication {
public static void main(String[] args) {
SpringApplication.run(wcasDeviceConnectionApplication.class, args);
}
}
My application properties are:
#datasource configurations
spring.datasource.url=MY DB URL
spring.datasource.username=MY DB USERNAME
spring.datasource.password=MY DB PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2-console
# DDL generation
spring.jpa.generate-ddl=true
logging.level.ch.qos.logback==DEBUG
spring.devtools.restart.enabled=true
debug=true
And when I enter http://localhost:8080/deviceConnection?verify=true
on the browser, I see this in the logs:
2022-06-16 19:00:00.078 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : GET "/deviceConnection?verify=true", parameters={masked}
2022-06-16 19:00:00.079 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-06-16 19:00:00.082 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error?verify=true", parameters={masked}
2022-06-16 19:00:00.083 DEBUG 19444 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-06-16 19:00:00.096 DEBUG 19444 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-06-16 19:00:00.097 DEBUG 19444 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
Can you please let me know what am I doing wrong. Any help is much appreciated.
CodePudding user response:
Your controller is in package com.gtt.wcas.device.root
but you setup @ComponentScan(basePackages = "com.gtt.wcas.device.details")
.
You controller is never picked up by Spring. Either move it to another package or change your component scan.
CodePudding user response:
Can you add com.gtt.wcas.device.root
in @ComponentScan
? and check ,
I've observed this issue with Spring boot , sometimes ,it does not identify endpoints if your Controller is in different package (other than source package) and not explicitly Scanned.
Also, you don't need @ResponseBody
on top of method definitions since @RestController
already has that definition!