im building CRUD app but ran into a problem . When I start the application at http://localhost:8080/api/v1/employees i recieve a whitelabel error 404 instead JSON response with db data .
Message
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Sep 08 17:49:26 GST 2022 There was an unexpected error (type=Not Found, status=404). No message available
Controller
package com.crudpet.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.crudpet.model.Employee;
import com.crudpet.repository.EmployeeRepository;
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeRepository.findAll();
}
}
main class
package com.crudpet.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class SpringbootBackendCrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootBackendCrudApplication.class, args);
}
}
console
2022-09-08 17:49:19.486 INFO 836 --- [ restartedMain] c.c.s.SpringbootBackendCrudApplication : Starting SpringbootBackendCrudApplication using Java 17.0.1 on DESKTOP-08M3S29 with PID 836 (C:\Users\Bogich\Documents\workspace-spring-tool-suite-4-4.13.1.RELEASE\springboot-backend-crud\target\classes started by Bogich in C:\Users\Bogich\Documents\workspace-spring-tool-suite-4-4.13.1.RELEASE\springboot-backend-crud)
2022-09-08 17:49:19.487 INFO 836 --- [ restartedMain] c.c.s.SpringbootBackendCrudApplication : No active profile set, falling back to 1 default profile: "default"
2022-09-08 17:49:19.556 INFO 836 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-09-08 17:49:19.556 INFO 836 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-09-08 17:49:20.206 INFO 836 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-09-08 17:49:20.223 INFO 836 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 JPA repository interfaces.
2022-09-08 17:49:20.833 INFO 836 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-09-08 17:49:20.844 INFO 836 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-09-08 17:49:20.844 INFO 836 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-08 17:49:20.917 INFO 836 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-09-08 17:49:20.917 INFO 836 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1360 ms
2022-09-08 17:49:21.132 INFO 836 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-09-08 17:49:21.193 INFO 836 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.10.Final
2022-09-08 17:49:21.388 INFO 836 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-09-08 17:49:21.507 INFO 836 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-09-08 17:49:21.684 INFO 836 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-09-08 17:49:21.701 INFO 836 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2022-09-08 17:49:21.988 INFO 836 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-09-08 17:49:21.999 INFO 836 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-09-08 17:49:22.063 WARN 836 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-09-08 17:49:22.450 INFO 836 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-09-08 17:49:22.492 INFO 836 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-09-08 17:49:22.502 INFO 836 --- [ restartedMain] c.c.s.SpringbootBackendCrudApplication : Started SpringbootBackendCrudApplication in 3.407 seconds (JVM running for 4.229)
2022-09-08 17:49:26.819 INFO 836 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-09-08 17:49:26.819 INFO 836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-09-08 17:49:26.820 INFO 836 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.crudpet</groupId>
<artifactId>springboot-backend-crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-backend-crud</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The problem is when i start app i must recieve a JSON response but i see Whitelabel error. I've tried ComponentScan , but it is not work for me. What i must do?
CodePudding user response:
You haven't specified the package name in your controller and main classes.
The default Spring auto-discovery won't work because of this, and your controller won't be instantiated. Spring looks for the components under the packages specified classes. Without the specified package, Spring won't be able to 'see' your beans and add them to ApplicationContext.
Simply add the package statement at the top of your class and everything should start working correctly.
Example:
package com.example;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.crudpet.model.Employee;
import com.crudpet.repository.EmployeeRepository;
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeRepository.findAll();
}
}
Updated:
Your main class SpringbootBackendCrudApplication
, which is marked as @SpringBootApplication
is located in the package com.crudpet.springboot
. Whereas your controller is located in the com.crudpet.controller
package.
By default, Spring Boot will scan for beans in the base package of the class with main method (SpringbootBackendCrudApplication
in your case) and all nested packages (e.g., com.crudpet.springboot.example
). The com.crudpet.controller
package with your controller class is out of scope and simply wasn't initiated.
To fix this, you need to place the SpringbootBackendCrudApplication
class in the com.crudpet
package, which is best practice. Another possible solution is explicitly specifying the com.crudpet.controller
package in the component scan.