1. 程式人生 > >springboot學習入門簡易版二---springboot2.0項目創建

springboot學習入門簡易版二---springboot2.0項目創建

pen contex depend 2.0 sun server cal started 自動

2 springboot項目創建(5)

環境要求:jdk1.8+

2.1創建maven工程

Group id :com.springboot

Artifact id: springboot2.0_first_demo

Packaging: jar

2.2pom文件配置

<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.springboot</groupId>
  <artifactId>springboot2.0_first_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- spring-boot-starter-parent 整合第三方常用框架依賴信息(包含各種依賴信息) -->
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.0.RELEASE</version>
  </parent>
    <!-- spring-boot-starter-web springboot整合springmvc web 
      實現原理:maven依賴繼承關系,相當於把第三方常用maven依賴信息,在parent項目中已封裝
  
--> <dependencies> <!-- 根據需要選擇parent中封裝的第三方框架 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 不需要寫版本號,因為在parent中已封裝好版本號 --> </dependency> </dependencies> </project>

2.3創建測試類及啟動類(6)

Springboot啟動原理:使用springmvc註解方式啟動(不需要xml配置文件)

2.3.1創建啟動類和測試類

簡潔起見,可將啟動類和controller在同一個類中(一般分別創建controller類和啟動類)

@RestController
@EnableAutoConfiguration //自動配置,根據pom文件引入的依賴信息,自動配置對應的組件
public class TestController {

    /**
     * @EnableAutoConfiguration
     *  註解作用:掃包範圍,默認在當前類中
     *  相當於 @SpringBootApplication
     * 
@return */ @PostMapping("/test") public String test(){ return "springboot2.0 first application"; } /** * 程序入口 * SpringApplication.run 相當於java代碼創建內置tomcat,加載springmvc註解啟動 * @param args */ public static void main(String[] args) { SpringApplication.run(TestController.class, args); } }

2.3.2啟動springboot項目

1TestController 類中,右鍵-->run asdebug as--java appspringboot app

啟動成功日誌:

2019-04-14 22:13:51.019 INFO 16108 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup

2019-04-14 22:13:51.102 INFO 16108 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘

2019-04-14 22:13:51.109 INFO 16108 --- [ main] com.springboot2.first.TestController : Started TestController in 6.615 seconds (JVM running for 9.788)

2 頁面訪問

http://localhost:8080/test

報錯:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Apr 14 22:15:23 CST 2019

There was an unexpected error (type=Method Not Allowed, status=405).

Request method ‘GET‘ not supported

Get方式不支持,修改類中的PostMappingRequestMapping,方便測試。

重啟再次訪問成功:

springboot2.0 first application

springboot學習入門簡易版二---springboot2.0項目創建