標籤:檢查 [] ping artifact tap 4.0 選擇 代碼產生 需要
SpringBoot入門Demo
SpringBoot可以說是Spring的簡化版。配置簡單、使用方便。主要有以下幾種特點:
- 建立獨立的Spring應用程式
- 嵌入的Tomcat,無需部署WAR檔案
- 簡化Maven配置
- 自動設定Spring
- 提供生產就緒型功能,如指標,健全狀態檢查和外部配置
- 絕對沒有代碼產生和對XML沒有要求配置
(1)建立maven項目,構建java項目(注意:這裡為java項目,也能通過內嵌的tomcat啟動服務,訪問controller指定路徑返回的資料)
項目目錄結構如下:
(需要編寫的只有三個檔案:User.java UserController.java pom.xml)
(2)User.java內容如下:(User.java主要是用於controller類要求方法中需要使用到User實體類)
package SpringBootDemo;public class User { private int id; private String username; private String password; public User(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String toString(){ return "user=[id:"+id+",username:"+username+",password:"+password+"]"; } }
(3)UserController類內容如下:(UserController相當於spring的controller類,但是必須使用@RestController註解標識)
@EnableAutoConfiguration :自動設定 這個註解應該放在SpringBoot項目的啟動類的上面 (含有main方法,並且調用了SpringApplication.run(類.class);方法)
@SpringBootApplication : 標明這是SpringBoot的啟動類
package SpringBootDemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * controller類 * @author Administrator * */@RestController@EnableAutoConfiguration@SpringBootApplication public class UserController { @RequestMapping("/login") public User login(){ User user= new User(); user.setId(123); user.setUsername("qwe"); user.setPassword("456"); return user; } @RequestMapping("/print") public String print1(){ return "HELLO WORLD"; }
//這是springBoot的啟動入口方法,也可以去建立一個類 ,在類裡面調用SpringApplication.run(UserController.class); public static void main(String[] args){ SpringApplication.run(UserController.class); }}
(4)pom.xml檔案的配置如下:裡面需要添加springBoot的包依賴
<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>springboot.maven</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <build> <finalName>springboot</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.1.RELEASE</version> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project><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>springboot.maven</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <build> <finalName>springboot</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.1.RELEASE</version> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
(5)啟動項目。
進入 UserController類中,右擊 --》選擇 Run As --》 Java Application
啟動成功後,會出現結果:
出現了 Started UserController。。。 這句話,標識啟動成功,然後就可以去瀏覽器訪問了
(6)訪問項目
由於沒有編寫設定檔,所有以訪問SpringBoot項目,預設是省略項目名的,直接使用 http://ip:port/RequestMapping對應的路徑
例如本例: http://127.0.0.1:8080/login
就會出現如下結果,:
SpringBoot入門樣本