SpringBoot2入门

64 阅读1分钟

1、系统要求

Build ToolVersion
SpringBoot2.7.1
Maven3.5+
Java8+

2.maven设置

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.1</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3.Hello, SpringBoot

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

  1. 创建maven工程
@RestController
@EnableAutoConfiguration
public class MyApplication {

    @RequestMapping("/")
    String home(){
        return "Hello,Spring Boot2~!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
  1. 测试 直接运行main方法,并在浏览器访问localhost:8080

4.创建可执行

把项目打成jar包,直接在目标服务器执行即可。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>