Java 教程中的Restful服务应用程序未在浏览器中运行:白标签错误页

Java 教程中的Restful服务应用程序未在浏览器中运行:白标签错误页,java,spring,rest,maven,Java,Spring,Rest,Maven,我一直在看一些Restful服务教程,现在我将继续学习 我已经根据它构建了应用程序(我正在使用eclipse和maven),方法是导入指南并从中获取代码,如中所示 . 为了运行它,我用一个main方法创建了应用程序类,构建了maven应用程序,但在浏览器中运行它时遇到了问题,因为我找不到它运行在哪个端口上,所以我创建了一个src/main/resources文件夹(顺便说一句,它丢失了)我添加了一个application.properties文件,其中包含server.port=8989。 现在

我一直在看一些Restful服务教程,现在我将继续学习 我已经根据它构建了应用程序(我正在使用eclipse和maven),方法是导入指南并从中获取代码,如中所示 .
为了运行它,我用一个main方法创建了应用程序类,构建了maven应用程序,但在浏览器中运行它时遇到了问题,因为我找不到它运行在哪个端口上,所以我创建了一个
src/main/resources
文件夹(顺便说一句,它丢失了)我添加了一个application.properties文件,其中包含
server.port=8989
。 现在,当我登录到(根据教程,这是应该显示应用程序的页面,因为我们正在使用
@RequestMapping(“/greeting”)
时,我得到一个

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
我在网上浏览了一下,但我上面链接的教程根本没有列出或提到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
问候:

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依赖项:我没有直接添加它,因为OP在他的项目中有一个html文件(一个视图),而我没有,我解释它的方式是,您只需要该依赖项来显示预先存在的视图(如果有的话)。此外,这是关键,后面的教程没有提到这种依赖关系。你会认为如果它如此重要,他们会加上它?! 最后,我将其添加到我的pom中,重新构建了应用程序,现在我得到了教程中所说的内容:我导航到/问候语,我在页面中看到:
{“id”:4,“content:“你好,世界!”}
。 因此,我在这里的想法是,不管怎样,您都需要这种依赖关系,并且它将是下次(不久)我(尝试)构建Restful服务时添加到pom中的第一件事。 感谢所有贡献者的帮助


编辑:我想我应该用我发现的其他东西来更新这个。教程没有说明要使用哪种Maven项目(阅读什么原型),所以我尝试了所有这些项目,结果不一。事实证明,使用“SpringStarter项目”,然后选择web依赖项似乎是最好的,这一点已经为你们中的一些人表示歉意。这为您提供了需要构建的restful服务所需的项目结构:它还为您提供了以前没有的src/main/resources文件夹(其中有一个空的application.property文件,您可以在其中指定端口),它为您提供了一个pom文件,其中包含所有相关的依赖项(在我不得不手动添加很多依赖项之前)而且,也许更重要的是,您不需要pom.xml中的thymeleaf依赖项,它在没有它的情况下工作(已测试)。希望这将对其他初学者有所帮助。

您应该在问题中发布您的代码,而不是使用外部链接,这就是stackoverflow的目的,不要害怕发布代码,默认端口是8080,似乎没有特定的页面可显示,这就是您获得回调的原因,因此您尝试使用thymeleaf作为渲染引擎?post does说您必须添加Maven依赖项。谢谢,已更新。是的,默认值是8080,但我无法在该端口上运行它,这就是为什么我尝试使用application.property文档对其进行更改。另外,据我所知,它不应该显示特定的页面,但是,如果您快速查看本教程的最后一部分,只显示json对象有了数据,我想这就是他们没有包括任何特定页面的原因。恐怕教程没有提到任何页面。另一个线程确实说要添加依赖项,但我认为这是因为OP在他的应用程序中有特定页面,而我没有。你的@RequestMapping在bean中吗?(@RestControler在类级别,或@Controller、@Component等)它与教程完全一样,带有
@RequestMapping
注释的方法位于带有
@RestController
的类中,我将使用您添加的文件编辑线程:org.springframework.boot spring boot starter thymeleaf我还发现了关于什么类型的应用程序需要c创建后,我已经用它更新了主线程,因为我认为它非常有趣和重要。感谢您发布此消息,@antobbo我整个下午都在努力解决这个问题。不幸的是,即使添加thymeleaf依赖项对我也不起作用。我现在休息一下,重新尝试“Spring starter项目”当我回来的时候,我想知道维护Spring指南文档的人是否知道人们在使用它时所经历的所有困难。对我来说,这肯定已经超过15分钟了,但现在还没有。也许如果我让它工作起来,我应该想办法联系他们并让他们知道。@Antobo-你的编辑说使用“Spring starter项目”,然后选择web依赖项。您能告诉我如何获取此“Spring starter项目”吗在Eclipse中创建Maven项目时,列表中是否有原型?默认情况下,目录中不显示原型,因此我想我需要原型目录URL来添加它。您是否使用默认情况下可能具有该原型的STS?
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);

    }

}