1、环境要求

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

1.1、maven设置

  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>

image.png

2、helloword

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

2.1、引入依赖

  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>

image.png

2.2、创建主程序

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

image.png

2.3、编写业务

  1. /**
  2. * 组合注解:@RestController = @ResponseBody + @Controller
  3. */
  4. @RestController
  5. public class HelloController {
  6. @RequestMapping("/hello")
  7. public String hello01(){
  8. return "Hello, Spring Boot 2!";
  9. }
  10. }

2.4、启动项目并测试

直接运行main方法,并在浏览器中输入地址:http://localhost:8080/hello,访问后出现以下页面表示访问成功,注意:默认访问地址的端口号为8080
02、SpringBoot 2.0 入门 - 图4image.png

2.5、编写配置文件简化配置application.properties

  1. #修改默认端口号
  2. server.port=8080

image.png

2.6、简化部署

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

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