I've recently started learning Spring and wanted to exercise a bit. I've created a simple spring boot application, so far it contains only 1 controller with single @GetMapping, but when I try to test it in my browser I only get Whitelabel error. Could someone hint me in the right direction?
Controller:
package com.calculate.calories.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
@RestController
public class RecipeController {
@GetMapping("/recipe")
public String getRecipes() {
HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.aniagotuje.pl/client/posts/search?categories=ciasta-i-torty&diets=&occasions=&tags=&page=0&sort=publish,desc"))
.GET()
.timeout(Duration.of(10, ChronoUnit.SECONDS))
.build();
HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.of(15, ChronoUnit.SECONDS)).build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return "e";
}
}
Main:
package com.calculate.calories.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SlimChefApplication {
public static void main(String[] args) {
SpringApplication.run(SlimChefApplication.class, args);
}
}
Main POM:
<?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>
<modules>
<module>config</module>
<module>persistence</module>
<module>common</module>
<module>application</module>
</modules>
<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.calculate.calories</groupId>
<artifactId>slimchef</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SlimChef</name>
<description>SlimChef</description>
<packaging>pom</packaging>
<properties>
<java.version>17</java.version>
<project.version>0.0.1-SNAPSHOT</project.version>
</properties>
</project>
POM of application module (where controller is):
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>slimchef</artifactId>
<groupId>com.calculate.calories</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>application</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
POM of config module (start point of Spring application):
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>slimchef</artifactId>
<groupId>com.calculate.calories</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.calculate.calories</groupId>
<artifactId>application</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.calculate.calories</groupId>
<artifactId>persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
CodePudding user response:
I can see 2 things "wrong" with your setup
- You have both
spring-boot-starter-web
and `spring-boot-starter-webflux in your dependencies. You generally use 1 or the other not both.
To fix remove one of the dependencies and decide what you want, or specify what Spring should start. For this you can use the spring.main.web-application-type
in your properties and set it to either SERVLET
or REACTIVE
.
- Your
@SpringBootApplication
is in thecom.calculate.calories.start
and it will, by default, scan only in the package it is defined in (and its subpackages). Your controller is incom.calculate.calories.web.controller
which doesn't match.
The best practice is to put your @SpringBootApplication
in a top level package, in your case com.calculate.calories
. If you don't you endup with a class with a lot of additional @ComponentScan
/@IMport
/@Enable*
annotations, which kind of beats the purpose of auto configuration and detection.
So in short move your @SpringBootApplication
class to com.calculate.calories
instead of a sub-package.