I get WhiteLabel error page with status 404 instead of String "All Movies!" I am making GET request on http://localhost:8080/api/v1/movies
My controller
package pl.krywion.movies.app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/movies")
public class MovieController {
@GetMapping
public String allMovies() {
return "All Movies!";
}
}
My main class
package pl.krywion.movies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MoviesApplication {
public static void main(String[] args) {
SpringApplication.run(MoviesApplication.class, args);
}
}
my pom.xml file
<?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>3.0.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.krywion</groupId>
<artifactId>movies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>movies</name>
<description>movies</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.paulschwarz</groupId>
<artifactId>spring-dotenv</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
my application.properties
spring.data.mongodb.database=${env.MONGO_DATABSE}
spring.data.mongodb.uri=mongodb+srv://${env.MONGO_USER}:${env.MONGO_PASSWORD}@${env.MONGO_CLUSTER}
My logs after Debug run and making a request
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@26512192
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager@1c16475
2023-02-26T19:45:33.143+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-02-26T19:45:33.143+01:00 INFO 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
2023-02-26T19:45:33.147+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/api/v1/movies", parameters={}
2023-02-26T19:45:33.152+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-26T19:45:33.157+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-02-26T19:45:33.157+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2023-02-26T19:45:33.161+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2023-02-26T19:45:33.162+01:00 DEBUG 8118 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-02-26T19:45:33.179+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json]
2023-02-26T19:45:33.179+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sun Feb 26 19:45:33 CET 2023, status=404, error=Not Found, message=No message available, (truncated)...]
2023-02-26T19:45:33.197+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
I don't know how to fix it, I am a Spring beginner
I tried using @ComponentScan annotation, and moving MovieApplication Java class to package above or moving it ot the same package as controller
There is an issue with component scanning in spring 3.0.3: https://github.com/spring-projects/spring-boot/issues/34379
The issue occurs when running your application not as a .jar
. If your project structure has directory elements whose names contain a characters that would be encoded in a URL, component scanning will not work as expected.
You can change your application's path to remove those characters. Or you downgrade to 3.0.2 or upgrade to 3.0.4, available since March 04, 2023.