1、系统要求

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

1.1、maven设置 设置阿里镜像和jdk1.8

  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.1、创建maven工程

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. // 返回对象为ConfigurableApplicationContext
  9. SpringApplication.run(MainApplication.class,args);
  10. }
  11. }

2.4、编写业务

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

image.png
image.png
image.png

2.5、测试

直接运行main方法

2.6、简化配置

application.properties(path resources/application.properties)

  1. server.port=8888

2.7、简化部署

  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>

把项目(包括依赖包和环境)打成jar包,直接在目标服务器执行即可。
通过maven打包成jar包,然后可以用java -jar xxxx.jar运行