【发布时间】:2021-11-13 18:31:40
【问题描述】:
我正在尝试运行一个简单的 Spring Boot。如果我将它设置为HelloWorldRest1Application.class,它确实可以运行和工作。将其更改为 HelloWorld 后,我在 localhost 上无法看到它。
2021-09-19 19:36:40.009 INFO 1664 --- [restartedMain] c.sw409.demo.HelloWorldRest1Application:开始 HelloWorldRest1Application 在 DESKTOP-55I895V 上使用 Java 16.0.1 PID 1664(C:\Users\ncost\Desktop\Fall 21\Advanced Java\java\HelloWorldRest1\target\classes 由 ncost 启动 C:\Users\ncost\Desktop\Fall 21\Advanced Java\java\HelloWorldRest1) 2021-09-19 19:36:40.013 信息 1664 --- [restartedMain] c.sw409.demo.HelloWorldRest1Application :没有活动配置文件集, 回退到默认配置文件:默认 2021-09-19 19:36:40.045 信息 1664 --- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor :DevTools 属性默认值 积极的!将“spring.devtools.add-properties”设置为“false”以禁用 2021-09-19 19:36:40.160 信息 1664 --- [restartedMain] c.sw409.demo.HelloWorldRest1Application:已启动 HelloWorldRest1Application 0.52 秒(JVM 运行 1.366)
主要
package com.sw409.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.sw409.HelloWorldRest.models.HelloWorldBean;
import com.sw409.HelloWorldRest.services.HelloWorld;
@SpringBootApplication
public class HelloWorldRest1Application {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class, args);
}
}
HelloWorldBean
package com.sw409.HelloWorldRest.models;
public class HelloWorldBean {
String mes;
public HelloWorldBean(String str) {
this.mes = str;
}
public String getMes() {
return mes;
}
public void setMes(String mes) {
this.mes = mes;
}
public String toString() {
return mes;
}
}
你好世界
package com.sw409.HelloWorldRest.services;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.sw409.HelloWorldRest.models.HelloWorldBean;
@RestController
public class HelloWorld {
@GetMapping("/hello")
public String helloworld() {
return ("hello world");
}
@GetMapping("/hello/{name}")
public String helloworld(@PathVariable("name") String str) {
return "hello " + str;
}
@GetMapping("/helloworldbean/{name}")
public HelloWorldBean hello(@PathVariable("name") String str) {
return new HelloWorldBean("Hello everyone! " + str);
}
}
【问题讨论】:
-
这是我得到的另一个错误:org.springframework.context.ApplicationContextException: Unable to start web server;嵌套异常是 org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean。
标签: java spring spring-boot