HTTP 状态 404 - 创建 springBoot 应用程序时未找到

Ank*_*ane 6 java spring spring-mvc spring-boot

这是一个 springboot 应用程序。它运行完美,但没有得到输出(它在浏览器中显示 HTTP 状态 404 错误)

pom.xml

<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>
  <groupId>com.exaample.demo</groupId>
  <artifactId>SpringBootMaven</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven spring boot project</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

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

    <properties>
        <java.version>1.8</java.version>   
    </properties>
</project>
Run Code Online (Sandbox Code Playgroud)

Springboot启动类 Main方法

package com.example.demo;

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

@SpringBootApplication
public class WebMainMethod {

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

    }

}
Run Code Online (Sandbox Code Playgroud)

控制器在主类之后加载

**Rest Controller**


package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHi() {
        return "Hi";
    }
}
Run Code Online (Sandbox Code Playgroud)

网址: http://localhost:8080/hello 输出

浏览器输出

此控制台日志 os springboot 应用程序

Nip*_*ana -2

您是否创建了一个名为 hi 的 jsp 或 html 页面...检查您的视图...您没有要查看的页面...请创建一个 jsp 并将其放在

   @RequestMapping("/hello")
        public String sayHi(Model model) {
        model.addAttribute("Hi","Hi")            
       return "Hi";
        }
Run Code Online (Sandbox Code Playgroud)

jsp页面必须是Hi

  • @NipunVidarshana OP使用`@RestController`,这意味着不会涉及视图模板解析器,它基本上是每个`@RequestMapping`上的`@ResponseBody`,所以没有模型的意义。Spring 甚至不会尝试使用“@RestController”查找 jsp/theamleaf/static 文件 (3认同)