我從Spring Initializr下載了一個Spring Boot專案。我試圖在DemoApplication.java中呼叫sayHello()/code>方法。以下是我的代碼。
DemoApplication.java
package com.example.demo。
import org.springframework.boot.SpringApplication。
import org.springframework.boot.autoconfigure.SpringBootApplication。
import org.springframework.web。 bind.annotation.GetMapping。
import org.springframework.web。 bind.annotation.RequestParam。
import org.springframework.web。 bind.annotation.RestController;
@SpringBootApplication.
@RestController; @SpringBootApplication
public class DemoApplication {
public static void main(span class="hljs-built_in">String[] args) {
System.out.println("started") 。
SpringApplication.run(DemoApplication.class, args) 。
System.out.println("Success") 。
}
@GetMapping("/hello"/span>)
public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) {
return String.format("Hello %s!", name) 。
}
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"/span>>
<modelVersion>/span>4.0.0</modelVersion>/span>
<parent>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>/span>2.5.4</version>
<relativePath/> <!--從版本庫中查找父本--> < >
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>/span>
<name>/span>demo</name>/span>
<description>Spring Boot的演示專案</description>
<properties>/span>
<java.version>1.8</java.version>
</properties>/span>
<dependencies>>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>/span>5.3.6</version>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<--<范圍>提供</scope>-->
</dependency>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>/span>
</dependencies>
<build>/span>
<plugins>/span>
<plugin>/span>
<groupId>org.springframework.boot</groupId>/span>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>/span>
</plugins>/span>
</build>/span>
</project>
當我在http://localhost:8080/hello,它回傳錯誤的404頁面。它沒有呼叫sayHello()方法,盡管我已經設定了GET映射。這可能是什么問題?
uj5u.com熱心網友回復:
問題在于你的依賴性,或者說缺乏依賴性。
你把spring-web作為一個依賴項,但這還不足以啟動你的網路應用。您還需要添加spring-webmvc,并且由于您想要使用REST(可能還有JSON),您需要添加jackson-databind依賴項(如果您想要支持日期,還需要添加jackson-datatype-jdk8依賴項)。
當然,你可以通過試驗和錯誤來手動計算出所有這些。或者您只需包含spring-boot-starter-web,它將自動包含所有這些依賴的(兼容)版本(包括嵌入式 Tomcat)。
因此,簡而言之,修復你的依賴性。
。
<dependencies>
<dependency>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>/span>
<dependency>>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>/span>
</dependencies>
這應該是你所需要的全部,如果你建立一個WAR而不是JAR,你可能需要添加
<dependency>/span>
<groupId>/span>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>/span>
這將使tomcat被提供并移出lib檔案夾,但你的應用程式仍然可以為開發而運行(通過main方法)并可作為戰爭部署(沒有tomcat在lib目錄中)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/332449.html
標籤:
上一篇:合并兩個多指標資料框架
下一篇:把2個物體放在物體里面
