我最近開始學習 Spring 并想鍛煉一下。我創建了一個簡單的 Spring Boot 應用程式,到目前為止它只包含 1 個帶有單個 @GetMapping 的控制器,但是當我嘗試在瀏覽器中測驗它時,我只收到 Whitelabel 錯誤。有人可以提示我正確的方向嗎?
控制器:
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";
}
}
主要的:
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);
}
}
主要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(控制器所在的位置):
<?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>
config模塊的POM(Spring應用的起點):
<?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>
截屏
uj5u.com熱心網友回復:
我可以看到您的設定有兩件事“錯誤”
- 您的依賴項中同時具有
spring-boot-starter-web和 `spring-boot-starter-webflux。您通常使用 1 或其他,而不是兩者。
要修復洗掉其中一個依賴項并決定您想要什么,或者指定 Spring 應該啟動什么。為此,您可以spring.main.web-application-type在屬性中使用 并將其設定為SERVLET或REACTIVE。
- 您
@SpringBootApplication在其中com.calculate.calories.start,默認情況下,它將僅在定義它的包(及其子包)中掃描。您的控制器在com.calculate.calories.web.controller其中不匹配。
最好的做法是把你@SpringBootApplication放在一個頂級包中,在你的情況下com.calculate.calories。如果你不這樣做,你最終會得到一個帶有很多附加@ComponentScan//注釋的類@IMport,@Enable*這會超出自動配置和檢測的目的。
因此,簡而言之,將您的@SpringBootApplication課程移至com.calculate.calories子包而不是子包。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510022.html
標籤:爪哇春天行家
