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" 路径了。

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站