搭建一个基本的 SpringBoot 项目。该项目只有一个类和一个方法。用@RestController
和@SpringBootApplication 注解类, 用@RequestMapping 注解方法。
先看我们需要准备那几个程序:
接下来开始开干!
创建BootApplication类:
package com.example.Boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
接着创建HelloControl类:
package com.example.Boot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControl {
@RequestMapping("/Boot")
public String index() {
return "Hello Spring Boot";
}
}
最后一步我们要设置端口号,端口号一般是8080,这里我用的是8081;
创建application.yml程序:
server:
port: 8081
spring:
bootapplication:
name:app
运行打开浏览器地址栏输入localhost:8081/Boot
打印出结果如图:
最后,感谢你的阅读,希望能帮助到你。