我開始學習Spring框架,在運行localhost:8080的時候得到這樣的錯誤
白色標簽的錯誤頁面 這個應用程式沒有明確的/error映射,所以你看到的是一個回退。
Controller.java
。package com.springFramework.helloGame.controller;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@GetMapping("/sum")
public long displaySum(){
回傳100。
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
HelloGameApplication.java
package com.springFramework.helloGame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
匯入org.springframework.context.annotation.ComponentScan。
@SpringBootApplication
public class HelloGameApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(HelloGameApplication.class, args)。
}
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
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.springFramework</groupId>
<artifactId>helloGame</artifactId>
<version>0.0.1-SNAPSHOT</version>/span>
<name>/span>helloGame</name>/span>
<description>Spring Boot的演示專案</description>
<properties>/span>
<java.version>11</java.version>/span>
</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.boot</groupId>
<artifactId>spring-boot-starter-web</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>/span>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我的專案結構看起來很好,但不知道為什么我得到了錯誤!請幫助我!
我的專案結構看起來很好,但不知道為什么我得到了錯誤。 請幫助我!
uj5u.com熱心網友回復:
看來你的控制器只有"/sum "路徑的映射。嘗試查詢localhost:8080/sum.
。uj5u.com熱心網友回復:
Whitelabel頁面是一個春季錯誤處理方法,它試圖提示你,你確實有一個/error的映射。 現在來討論這個問題,你需要在控制器的get映射上面寫@RequestMapping("/"),這樣spring就知道基礎URI。 這是一個瀏覽器的spring應用程式的入口點。
現在你已經寫了/sum獲取URI,在瀏覽器中,你正在嘗試獲取http://localhost:8080。 所以為了使上述URI發揮作用,你需要提供@RequestMapping("/")
或者你可以使用 http://localhost:8080/sum 與你目前的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320488.html
標籤:

