java - 教程中的 Restful 服务应用程序未在浏览器中运行 : Whitelabel Error Page

标签 java spring rest maven

我一直在看一些 Restful 服务教程,现在我正在关注 this one 我已经根据它构建了应用程序(我正在使用 Eclipse 和 Maven),方法是导入指南并从那里获取代码,如 here 中所示。 。
为了运行它,我使用 main 方法创建了 Application 类,构建了 Maven 应用程序,但在浏览器中运行它时遇到了问题,因为我找不到它运行的端口,所以我创建了一个 src/main/resources 文件夹(顺便说一下,该文件夹丢失了),我添加了一个 application.properties 文件,其中包含 server.port = 8989 。 现在,当我登录http://localhost:8989/greeting时(根据教程,这是应该显示应用程序的页面,因为我们使用的是 @RequestMapping("/greeting")) 我得到了

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

我上网查了一下,遇到this thread但我上面链接的教程根本没有列出或提及 thymeleaf 依赖项,而且我没有任何像该线程中的 OP 这样的 HTML 文件,这就是我在这里发布的原因。

控制台日志也很有趣,因为它有一个关于我的 pom.xml 文件的警告: “[警告]请求的配置文件“pom.xml”无法激活,因为它不存在”:
它也提到了我创建的文件夹的一些内容: “跳过不存在的资源目录 C:\workspace\gs-consuming-rest-initial\src\test\resources”。

这是完整的日志:

[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-consuming-rest 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-consuming-rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-consuming-rest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gs-consuming-rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\workspace\gs-consuming-rest-initial\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gs-consuming-rest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest ---

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

2017-01-19 14:09:57.701  INFO 23288 --- [           main] hello.Application                        : Starting Application on XXXXXX with PID XXXXXX (C:\workspace\gs-consuming-rest-initial\target\classes started by XXXXXX in C:\workspace\gs-consuming-rest-initial)
2017-01-19 14:09:57.704  INFO 23288 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2017-01-19 14:09:57.738  INFO 23288 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c2fd2b6: startup date [Thu Jan 19 14:09:57 GMT 2017]; root of context hierarchy
2017-01-19 14:09:58.331  INFO 23288 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-01-19 14:09:58.338  INFO 23288 --- [           main] hello.Application                        : Started Application in 0.983 seconds (JVM running for 3.448)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.714 s
[INFO] Finished at: 2017-01-19T14:09:58+00:00
[INFO] Final Memory: 26M/327M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
2017-01-19 14:09:58.399  INFO 23288 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c2fd2b6: startup date [Thu Jan 19 14:09:57 GMT 2017]; root of context hierarchy
2017-01-19 14:09:58.400  INFO 23288 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

为了完整性,我的 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>org.springframework</groupId>
    <artifactId>gs-consuming-rest</artifactId>
    <version>0.1.0</version>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>


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

</project>

由于我是 Restful 服务的新手,还有什么教程没有提到的我应该做的事情吗?

由于文件相当短,我将包括它们: 问候 Controller :

package hello;

import java.util.concurrent.atomic.AtomicLong;

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

@RestController
public class GreetingController
{
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    //@RequestMapping maps all HTTP operations
    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

问候:

package hello;

public class Greeting
{
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

应用:

package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{

    public static void main(String[] args)
    {
        System.out.println("App Started");
        SpringApplication.run(Application.class, args);

    }

}

最佳答案

好吧,看来我最终成功了。 就我而言,这是多种因素的结合:首先是端口,我不断地随机更改端口,然后选择了 8900,但我确信任何端口都可以。

然后这个thymeleaf依赖项:我没有直接将其添加为suggested in here只是因为那里的OP在他的项目中有一个html文件(一个 View ),而我没有,而且我的解释方式是,如果您需要该依赖项来显示预先存在的 View 有什么。此外,这是关键,后面的教程没有提到这种依赖关系。您会认为如果它如此重要,他们会添加它吗?! 最后,我将其添加到我的 pom 中,重新构建了应用程序,现在我明白了教程所说的内容:我导航到/greeting 并在页面中看到: {"id":4,"content":"你好,世界!"}。 因此,我的看法是,无论如何,您都需要这种依赖关系,并且它将是下次(很快)我(尝试)构建 Restful 服务时首先添加到我的 pom 中的东西。 感谢所有贡献者的帮助

编辑:我想我应该用我发现的其他东西来更新它。本教程没有说明要使用哪种 Maven 项目(阅读什么原型(prototype)),因此我尝试了所有这些,结果好坏参半。事实证明 - 并向那些已经知道这一点的人道歉 - 似乎最好使用“Spring 启动项目”,然后选择 Web 依赖项。这为您提供了构建所需的 Restful 服务所需的项目结构:它还为您提供了以前不存在的 src/main/resources 文件夹(带有一个空的 application.property 文件,您可以在其中指定端口),它为您提供了一个包含所有相关依赖项的 pom 文件(在我必须手动添加相当多的依赖项之前),也许更重要的是,您不需要 pom.xml 中的 thymeleaf 依赖项,它可以在没有它的情况下工作(经过测试) 。希望这会对其他初学者有所帮助。

关于java - 教程中的 Restful 服务应用程序未在浏览器中运行 : Whitelabel Error Page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41744114/

相关文章:

php - 在 PHP 中发出 REST API 请求

c# - Asp.Net Web Api 在 Godaddy 服务器上不工作,但在本地工作正常

来自 Star SDK 的 starIOport.jar 上的 java.lang.VerifyError

java - 如何检索按钮位置?

java - 如何在manyToMany字段中实现 View ?

java - 如何为一个完整的线程创建日志

web-services - 如何指定 webServiceTemplate 使用哪种版本的 soap?

Java Applet 无法在网页上加载

java - 找到一个具体的实例,最好的方法是什么

c# - 来自 ASP.NET 2.0 的 REST