简体   繁体   中英

Spring Boot returns 404

I would like to get a running Springboot Application with Java Backend.

I tried an example application of the web. I runs on Tomcat. I can install the maven, and run the jar. I can open the Localhost:8082/ and it returns some.json document.

What i cannot do is to jump to some.html page. Is there an error in my structure?

Strucure:

在此处输入图像描述

HomeController.java

package com.example.demo.controller;

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

@Controller
public class HomeController {
    @RequestMapping("/home")
   public String home() {
       return "index";
   }
   @RequestMapping("/test")
   public String test() {
       return "test";
   }
}

application.properties:

spring.mvc.view.prefix = /views/
spring.mvc.view.suffix = .html
server.port=8082

Application:

@SpringBootApplication
public class BootDemoApplication  {
   @Autowired
   UserRepository userRepository;

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

Error: 在此处输入图像描述

Run-Output:

在此处输入图像描述

The mapping should work:

2020-12-03 10:09:23.444  INFO 4764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test]}" onto public java.lang.String com.example.demo.controller.HomeController.test()
2020-12-03 10:09:23.446  INFO 4764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/home]}" onto public java.lang.String com.example.demo.controller.HomeController.home()

I have tried to change the application.properties into:

 spring.mvc.view.prefix = resources/static/views/

and

 spring.mvc.view.prefix = /static/views/

this had no effect on the result.

Is there an error, i cannot see?

Thanks

MY 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>

<groupId>com.example</groupId>
<artifactId>bootdemo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>bootDemo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.3.4.RELEASE</version>
        <type>maven-plugin</type>
    </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>

fix application properties.

spring:
    mvc:
       view:
        suffix : .html

and call url: http://localhost:8082/views/index

try to add @ComponentScan to the main class @SpringBootApplication @ComponentScan(basePackageClasses = HomeController.class) public class BootDemoApplication {

You can use thymeleaf as template engine and put your views under the templates folder src/main/resources > templates, thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM