1、系统要求

  • Java 8 & 兼容java14 .
  • Maven 3.3+
  • idea 2019.1.2

    1.1、maven设置

    设置Maven Jdk版本,Maven仓库首选下载位置(阿里云)
    apache-maven-3.6.3\conf\setting.xml

    1. <mirrors>
    2. <mirror>
    3. <id>nexus-aliyun</id>
    4. <mirrorOf>central</mirrorOf>
    5. <name>Nexus aliyun</name>
    6. <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    7. </mirror>
    8. </mirrors>
    9. <profiles>
    10. <profile>
    11. <id>jdk-1.8</id>
    12. <activation>
    13. <activeByDefault>true</activeByDefault>
    14. <jdk>1.8</jdk>
    15. </activation>
    16. <properties>
    17. <maven.compiler.source>1.8</maven.compiler.source>
    18. <maven.compiler.target>1.8</maven.compiler.target>
    19. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    20. </properties>
    21. </profile>
    22. </profiles>

    2、HelloWorld

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

    2.2、引入依赖

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.3.4.RELEASE</version>
    5. </parent>
    6. <dependencies>
    7. <dependency>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-web</artifactId>
    10. </dependency>
    11. </dependencies>

    2.3、创建主程序

    1. /**
    2. * 主程序类
    3. * @SpringBootApplication:这是一个SpringBoot应用
    4. */
    5. @SpringBootApplication
    6. public class MainApplication {
    7. public static void main(String[] args) {
    8. SpringApplication.run(MainApplication.class,args);
    9. }
    10. }

    2.4、编写业务

    1. @RestController //@RestController == @ResponseBody + @RequestMapping
    2. public class HelloController {
    3. @RequestMapping("/hello")
    4. public String handle01(){
    5. return "Hello, Spring Boot 2!";
    6. }
    7. }

    2.5、测试

    直接运行main方法

    2.7、简化部署

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

    1. <!--maven的插件-->
    2. <build>
    3. <plugins>
    4. <plugin>
    5. <groupId>org.springframework.boot</groupId>
    6. <artifactId>spring-boot-maven-plugin</artifactId>
    7. </plugin>
    8. </plugins>
    9. </build>