第一个SpringBoot应用程序

2021/7/6 17:39:27

本文主要是介绍第一个SpringBoot应用程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 创建POM

创建一个maven工程,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>com.ttpfx</groupId>
    <artifactId>sb01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

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

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

2. 创建主程序

package com.ttpfx.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

3. 创建HelloController

package com.ttpfx.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle() {
        return "Hello SpringBoot!";
    }
}

4. 运行程序

4.1 在IDEA中运行MainApplication:

4.2 浏览器访问http://localhost:8080/hello:

5. 打成jar包

5.1 pom.xml中增加打包配置:

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

5.2 IDEA中打包:

5.3 运行jar包

5.4 浏览器访问http://localhost:8080/hello:

6. 配置文件

6.1 src/main/resources/下增加application.properties配置文件:

server.port=8888

6.2 运行MainApplication,浏览器访问http://localhost:8888/hello:

更多配置参考:https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties



这篇关于第一个SpringBoot应用程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程