教程中的静态服务应用程序未在浏览器中运行:Whitelabel错误页面 [英] Restful service application from tutorial not running in browser: Whitelabel Error Page

查看:98
本文介绍了教程中的静态服务应用程序未在浏览器中运行:Whitelabel错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看一些Restful服务教程,现在我正在关注此一个 我已经通过导入指南并按照此处 .
要运行它,我用一个主要方法创建了Application类,并构建了maven应用程序,但是由于找不到运行在哪个端口上而在浏览器中运行它时遇到了问题,所以我创建了一个src/main/resources文件夹(顺便说一句),我添加了一个带有server.port = 8989的application.properties文件. 现在,当我登录到 http://localhost:8989/greeting 时(根据本教程,这是该页面应显示该应用程序,因为我们正在使用@RequestMapping("/greeting"))我得到

I’ve been looking at some Restful services tutorial, and now I’m following this one I’ve built the application according to it (I’m using eclipse and maven) by importing the guide and taking the code from there as in here .
To run it I created the Application class with a main method, built the maven application but I had problems running it in the browser because I couldn’t find which port it was running on, so I created a src/main/resources folder (which was missing by the way) and I added an application.properties file with server.port = 8989 in it. Now, when I log on to http://localhost:8989/greeting (according to the tutorial this is the page that should display the application because we’re using @RequestMapping("/greeting")) I get a

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

我在网上看过一次,遇到了此线程,但该教程我链接到上面的链接根本没有列出或提及百里香属植物的依赖性,并且在该线程中没有像OP这样的HTML文件,这就是我在这里发布的原因.

I had a look online, run into this thread but the tutorial I linked to above doesn’t list or mention the thymeleaf dependency at all, and I don’t have any HTML file like the OP in that thread, that’s why I posted here.

控制台日志也很有趣,因为它有关于我的pom.xml文件的警告: [WARNING] The requested profile "pom.xml" could not be activated because it does not exist:
并且确实提到了有关我创建的文件夹的一些信息: skip non existing resourceDirectory C:\workspace\gs-consuming-rest-initial\src\test\resources.

Also the console log is interesting, as it has a warning about my pom.xml file: "[WARNING] The requested profile "pom.xml" could not be activated because it does not exist":
and it does mention something about the folder that I’ve created too: "skip non existing resourceDirectory 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在这里:

And my pom.xml is here for completeness:

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

由于我不熟悉静态服务,因此该教程未提及的其他事情我应该做吗?

As I’m new to restful services, is there anything else I should do that the tutorial isn’t mentioning?

由于文件很短,我将它们包括在内: GreetingController:

As the files are rather short, I'll include them: GreetingController:

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,但我敢肯定任何事情都会发生.

然后是这种百里香的依赖:我没有直接将其添加为建议在这里仅仅是因为OP在他的项目中有一个html文件(视图),而我没有,并且我的解释方式是,您只需要依赖项来显示已有的视图(如果有).此外,这是关键,后面的教程没有提到这种依赖性.您会认为,如果它是如此重要,他们会添加它吗? 最后,我将其添加到pom中,重新构建了应用程序,现在我得到了教程中所说的内容:我导航至/greeting,并在页面中获得了该内容: {"id":4,"content":"Hello, World!"}. 因此,对我而言,无论如何,您都需要这种依赖关系,这将是下次(不久)我(试图)构建一个Restful服务添加到pom中的第一件事. 感谢所有贡献者的帮助

我以为我会用发现的其他东西来更新它.本教程没有说明要使用哪种Maven项目(阅读哪种原型),因此我尝试了所有混合结果.事实证明-对已经知道这一点的人表示歉意-似乎最好使用"Spring starter Project",然后选择Web依赖项.这为您提供了构建所需的静态服务所需的项目结构:它还为您提供了以前没有的src/main/resources文件夹(带有一个空的application.property文件,您可以在其中指定端口),它为您提供了一个包含所有相关依赖项的pom文件(在我不得不手动添加很多依赖项之前),并且也许更重要的是,您将不需要pom.xml中的thymeleaf依赖项,如果没有它,它就可以工作(经过测试) .希望这会对其他初学者有所帮助.

OK, it seems I manage to get it to work in the end. In my case, it was a combination of things: first the port, I kept changing port at random and then settled for 8900, but I'm sure that any would do.

Then this thymeleaf dependency: I didn't add it straightaway as suggested in here simply because the OP there had an html file (a view) in his project whereas I didn't and the way I interpreted it was that you need that dependency only to display the pre-existing view if you have any. Besides, and this is key, the tutorial followed does not mention this dependency. You'd think that if it was so important they would add it?! In the end I added it to my pom, rebuilt the app and now I get what the the tutorial say: I navigate to /greeting and I get this in the page: {"id":4,"content":"Hello, World!"}. So the take for me here is that, no matter what, you need that dependency and it'll be the first thing to add to my pom next time (shortly) I (attempt to) build a Restful service. Thanks to all the contributors for the help

I thought I'd update this with something else I discovered. The tutorial doesn't say what kind of Maven project (read what archetype) to use, so I tried all of them with mixed results. It turned out - and apologies to those of you who know that already - that it seems best to use a "Spring starter Project" and then select the web dependency. This gives you the project structure you need for the restful service you need to build: it also gives you the src/main/resources folder which wasn't there before (with an empty application.property file where you can specify the port), it gives you a pom file complete with all the relevant dependencies (before I had to add quite a few dependencies manually) and, perhaps more importantly, you will not need the thymeleaf dependency in the pom.xml, it works without it (tested). Hopefully this will help other beginners.

这篇关于教程中的静态服务应用程序未在浏览器中运行:Whitelabel错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆