sw_eet 2019-11-23 17:52 采纳率: 0%
浏览 2087

Whitelabel Error Page,很急

使用http://localhost:8887/hello?name=community
**返回

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback
Sat Nov 23 17:42:18 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

**编译器run

2019-11-23 17:38:20.780 INFO 9072 --- [ main] c.x.community.CommunityApplication : Starting CommunityApplication on SW-PC with PID 9072 (G:\community\target\classes started by SW in G:\community)
2019-11-23 17:38:20.784 INFO 9072 --- [ main] c.x.community.CommunityApplication : No active profile set, falling back to default profiles: default
2019-11-23 17:38:22.429 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8887 (http)
2019-11-23 17:38:22.440 INFO 9072 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-23 17:38:22.440 INFO 9072 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-23 17:38:22.518 INFO 9072 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1678 ms
2019-11-23 17:38:22.750 INFO 9072 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-23 17:38:23.136 INFO 9072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8887 (http) with context path ''
2019-11-23 17:38:23.140 INFO 9072 --- [ main] c.x.community.CommunityApplication : Started CommunityApplication in 2.996 seconds (JVM running for 4.612)

比老师多了这几行
2019-11-23 17:38:35.007 INFO 9072 --- [nio-8887-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-23 17:38:35.008 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-23 17:38:35.016 INFO 9072 --- [nio-8887-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms**
图片说明
代码

G:\community\src\main\java\com\xjuygur\community\controller\HelloController.java

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController{

    @GetMapping("/hello")
    public String hello(@RequestParam(name="name") String name, Model model){
        model.addAttribute("name",name);
        return "hello";
    }
}

**html

G:\community\src\main\resources\templates\hello.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

pom.xml

G:\community\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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xj-uygur</groupId>
    <artifactId>community</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>community</name>
    <description>community</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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

  • 写回答

1条回答 默认 最新

  • Kwan的解忧杂货铺 Java领域优质创作者 2024-01-27 19:35
    关注

    参考chatgpt
    从您提供的信息来看,您的Spring Boot应用已经成功启动并监听在 http://localhost:8887 上。然而,当您尝试访问 http://localhost:8887/hello?name=community 时,系统返回了404错误。

    这种情况通常是因为您的应用中没有映射到路径 "/hello" 的控制器方法,导致Spring Boot找不到对应的处理器。为了解决这个问题,您可以创建一个控制器类,并在其中添加一个方法,以映射到 "/hello" 路径。

    下面是一个简单的示例:

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @GetMapping("/hello")
        public String sayHello(@RequestParam String name) {
            return "Hello, " + name + "!";
        }
    }
    

    这个控制器类包含了一个映射到 "/hello" 路径的方法 sayHello,并接受一个名为 "name" 的请求参数。在这个例子中,它返回一个简单的问候语。

    确保您的控制器类位于Spring Boot应用的扫描路径下,或者使用 @ComponentScan 注解来扫描该类。然后重新运行应用,访问 http://localhost:8887/hello?name=community,您应该能够看到相应的问候语。

    如果您的应用还没有定义 main 方法的入口类,您可能需要创建一个类并添加 @SpringBootApplication 注解:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CommunityApplication {
        public static void main(String[] args) {
            SpringApplication.run(CommunityApplication.class, args);
        }
    }
    

    这样,您的应用就可以正确地处理 "/hello" 路径了。

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题