【发布时间】:2017-06-04 06:56:29
【问题描述】:
我一直在看一些 Restful 服务教程,现在我在关注 this one
我已经根据它构建了应用程序(我正在使用 eclipse 和 maven),方法是导入指南并从那里获取代码,如here
.
为了运行它,我使用 main 方法创建了 Application 类,构建了 maven 应用程序,但是我在浏览器中运行它时遇到了问题,因为我找不到它在哪个端口上运行,所以我创建了一个 src/main/resources 文件夹(顺便说一下丢失了),我添加了一个带有server.port = 8989 的application.properties 文件。
现在,当我登录到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 文件的警告:
“[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”.
这是完整的日志:
[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 服务的新手,还有什么我应该做的教程没有提到的吗?
由于文件很短,我将它们包括在内: 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);
}
}
【问题讨论】:
-
你应该在你的问题中发布你的代码而不是使用外部链接,这就是stackoverflow的用途,不要害怕发布代码,默认端口是8080,似乎没有特定的页面可以显示这就是为什么你得到回调,所以你试图使用 thymeleaf 作为渲染引擎?该帖子确实说您必须添加 Maven 依赖项。
-
谢谢,已更新。是的,默认值为 8080,但我无法在该端口上运行它,这就是我尝试使用 application.property 文档更改它的原因。另外,据我了解,它不应该显示特定页面,但是,如果您快速浏览本教程的结尾,只有一个带有数据的 json 对象,我想这就是为什么它们不包含任何特定页面。恐怕该教程没有提到任何页面。另一个线程确实说要添加该依赖项,但我认为这是因为 OP 在他的应用程序中有一个特定页面,而我没有。
-
你的 @RequestMapping 在一个 bean 中吗? (类级别的@RestControler,或者@Controller、@Component等)
-
和教程一模一样,带有
@RequestMapping注释的方法位于带有@RestController的类中,我也会用文件编辑线程