SpringBoot之demo

官網地址:https://projects.spring.io/spring-boot/java

說明:網上好多資源中springboot都是經過maven工具構建的,固然我也不例外,作第一個小demo前,先給你的工具構建maven的環境。web

1.建立maven工程:項目結構以下spring

    說明:我建立了好屢次項目結構老是不完整而且報錯,項目-->右鍵 build path 添加 tomcat 便可解決。apache

   經過次鏈接能夠解決上述問題:https://blog.csdn.net/shunhua19881987/article/details/79270925瀏覽器

 2.項目結構說明  tomcat

     src/main/java 程序開發以及主程序入口
     src/main/resources 配置文件
     src/test/java 測試程序
     pom.xml文件是Maven進行工做的主要配置文件。springboot

 3.搭建項目的骨架 mvc

      

Application.java  代碼以下app

 1 package com.cn;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Application.class, args);
11     }
12 }

FirstDemoController.java  代碼以下jsp

 1 package com.cn.controller;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 @RestController
 7 public class FirstDemoController {
 8 
 9     @RequestMapping("/first")
10     public String index() {
11         return "This is my first demo!";
12     }
13 
14 }

在target目錄下找到pom.properties文件,添加以下內容

 1 #Generated by Maven Integration for Eclipse
 2 #Wed Mar 14 21:54:44 CST 2018
 3 version=0.0.1-SNAPSHOT
 4 groupId=com.cn
 5 m2e.projectName=SpringBootDemo
 6 m2e.projectLocation=E\:\\workspace\\SpringBootDemo
 7 artifactId=SpringBootDemo
 8 
 9 spring.mvc.view.prefix=/pages
10 spring.mvc.view.suffix=.jsp

pom.xml 文件以下

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.cn</groupId>
 5     <artifactId>SpringBootDemo</artifactId>
 6     <packaging>war</packaging>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>SpringBootDemo Maven Webapp</name>
 9     <url>http://maven.apache.org</url>
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>1.5.1.RELEASE</version>
14     </parent>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter-web</artifactId>
20         </dependency>
21         
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-devtools</artifactId>
25             <optional>true</optional>
26         </dependency>
27 
28         <dependency>
29             <groupId>junit</groupId>
30             <artifactId>junit</artifactId>
31             <version>3.8.1</version>
32             <scope>test</scope>
33         </dependency>
34     </dependencies>
35     <build>
36         <finalName>SpringBootDemo</finalName>
37         <plugins>
38             <plugin>
39                 <groupId>org.springframework.boot</groupId>
40                 <artifactId>spring-boot-maven-plugin</artifactId>
41                 <configuration>
42                     <fork>true</fork>
43                 </configuration>
44             </plugin>
45         </plugins>
46     </build>
47 </project>

4.運行項目:來到主函數:Application.java  右鍵  --->Run as -->java Application

瀏覽器請求:http://localhost:8080/SpringBootDemo/first

 

 

。。。。。。。未完待續。

相關文章
相關標籤/搜索