簡體   English   中英

Spring boot 上的基本控制器給出 404

[英]Basic controller on Spring boot giving 404

我正在嘗試在 spring boot 上創建一個基本控制器,但我得到:

白標錯誤頁面

此應用程序沒有針對 /error 的顯式映射,因此您將其視為后備。

Fri Dec 14 21:21:36 ART 2018 出現意外錯誤(類型=未找到,狀態=404)。

沒有可用的消息

錯誤。 我將控制器放在應用程序所在的包的子包中。

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 http://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>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

家庭控制器.java:

package com.example.demo.home;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home(){
        return "home";
    }

}

演示應用程序.java:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

控制台輸出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2018-12-14 21:16:46.179  INFO 5420 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on DESKTOP-BMTCH27 with PID 5420 (C:\Users\Usuario\eclipse-workspace\Project\target\classes started by Usuario in C:\Users\Usuario\eclipse-workspace\Project)
2018-12-14 21:16:46.179  INFO 5420 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-12-14 21:16:51.395  INFO 5420 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-12-14 21:16:51.433  INFO 5420 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-14 21:16:51.433  INFO 5420 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-14 21:16:51.464  INFO 5420 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_192\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_192/bin/server;C:/Program Files/Java/jre1.8.0_192/bin;C:/Program Files/Java/jre1.8.0_192/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\Tools\Binn\;c:\Program Files\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;c:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps;;C:\Users\Usuario\Desktop;;.]
2018-12-14 21:16:51.871  INFO 5420 --- [           main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2018-12-14 21:16:51.871  INFO 5420 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-12-14 21:16:51.871  INFO 5420 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5489 ms
2018-12-14 21:16:52.449  INFO 5420 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-14 21:16:52.959  INFO 5420 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-14 21:16:52.975  INFO 5420 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 8.155 seconds (JVM running for 9.247)
2018-12-14 21:17:00.448  INFO 5420 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-14 21:17:00.448  INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2018-12-14 21:17:00.462  INFO 5420 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 13 ms

文件夾結構

它可以來自@Controller 注釋。 請將其更改為@RestController

來自 spring boot 文檔https://spring.io/guides/gs/rest-service/

在 Spring 構建 RESTful Web 服務的方法中,HTTP 請求由控制器處理。 這些組件可以通過 @RestController 注釋輕松識別,下面的 GreetingController 通過返回 Greeting 類的新實例來處理對 /greeting 的 GET 請求:

@Controller注釋表示 Spring MVC 控制器。 如果您使用@Controller注釋標記您的類,則必須將@ResponseBody放在處理程序方法上。

另一方面, @RestController注解是@Controller的特殊版本,它會自動添加@Controller@ResponseBody注解,因此無需將@ResponseBody添加到我們的處理程序方法中。

@RestController
public class HomeController {

    @RequestMapping("/")
    public String home(){
        return "home";
    }

}

要么

@Controller
public class HomeController {

    @RequestMapping("/")
    @ResponseBody
    public String home(){
        return "home";
    }

}

有關信息:如果您未指定任何映射,此方法將解析所有 http 請求,即您可以將GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE請求發送到指定的 url,它將被解析。

對於你的情況,你只改變你的控制器看起來像:

package com.example.demo.home;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

    @RestController
    public class HomeController {

        @RequestMapping("/") // Or you can do : @GetMapping("/")
        public String home(){
            return "home";
        }

    }

網址:

http://localhost:8080/

對於您的代碼/配置,它將在staticwebapp文件夾下查找html文件,同時您的家庭控制器應該返回home.html而不是home

Thymeleaf 應該作為依賴項包含在項目的 pom.xml 中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

此外,home.html 文件應從模板移動到靜態目錄。

有關更多詳細信息,請查看使用 Spring MVC 提供 Web 內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM