我正在尝试通过
为REST服务创建示例https://spring.io/guides/gs/rest-service/
我已经创建了所有3个班级
我正在共享代码。我看到了另一个问题,不同的软件包存在问题,但我的班级在同一个软件包中。
运行程序后,我得到以下结果:
我看到控制台显示终止,所以我不知道这是为什么我无法访问localhost:8080 / greeting的原因,并且我无法访问该站点
此项目是从Spring Tool 4构建的,原因是在Eclipse中尝试从“项目”中选择“导入Spring入门内容”时出现错误,但这是第二个问题。
控制台显示下一个:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-11-12 19:03:36.968 INFO 20324 --- [ main] hello.Application : Starting Application on ACL_GRE_PavlidisK with PID 20324 (C:\Users\KyriakosP\WorkSpaces\SpringTool\gs-consuming-rest-initial\target\classes started by KyriakosP in C:\Users\KyriakosP\WorkSpaces\SpringTool\gs-consuming-rest-initial)
2018-11-12 19:03:36.968 INFO 20324 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2018-11-12 19:03:37.015 INFO 20324 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@21a947fe: startup date [Mon Nov 12 19:03:37 CET 2018]; root of context hierarchy
2018-11-12 19:03:37.701 INFO 20324 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-11-12 19:03:37.705 INFO 20324 --- [ main] hello.Application : Started Application in 0.991 seconds (JVM running for 1.338)
2018-11-12 19:03:37.705 INFO 20324 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@21a947fe: startup date [Mon Nov 12 19:03:37 CET 2018]; root of context hierarchy
2018-11-12 19:03:37.705 INFO 20324 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-
plugin:2.0.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-consuming-rest'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile("junit:junit")
}
我没有更改gradle文件。教程中没有有关Gradle文件的任何内容
在您建议添加后
compile("org.springframework.boot:spring-boot-starter-web")
在gradle文件中,当我从IDE运行应用程序时,程序终止。
与命令中的maven相同。
只有命令行中的gradle(gradle bootRun)可以工作并保持运行,因此浏览器可以响应。那么在这些情况下它杀死了该应用程序是什么?
答案 0 :(得分:1)
尝试更换gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-
plugin:2.0.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-consuming-rest'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.fasterxml.jackson.core:jackson-databind")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
正如spring boot文档中所述,您还应该在根包中的其他类之上找到主应用程序类。只是一个想法,您可以继续努力。
答案 1 :(得分:0)
您可能要指定RequestMethod的方法:
例如
@RequestMapping(value = "/greeting", method = RequestMethod.GET)
或
@GetMapping("/greeting")
除此之外,它看起来让我感到困惑。
您可能要查看build.gradle文件中是否包含Web Starter。
在一个文件DemoGradleApplication
中的示例:
package com.example.demogradle;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;
@SpringBootApplication
public class DemoGradleApplication {
public static void main(String[] args) {
SpringApplication.run(DemoGradleApplication.class, args);
}
}
@RestController
class HelloWorldController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public Message helloWorld() {
return new Message(UUID.randomUUID()
.toString(), "Hello, world!");
}
@GetMapping("/hello/{id:.*$}")
public Message hello(@PathVariable("id") String msg) {
return new Message(UUID.randomUUID()
.toString(), msg);
}
}
@Data
@AllArgsConstructor
class Message {
private String id;
private String message;
}
带有gradle文件:
buildscript {
ext {
springBootVersion = '2.1.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
运行方式:
gradlew bootRun
在浏览器中转到URL http://localhost:8080/hello
或http://localhost:8080/hello/you all rocking people