SpringBoot入門demo

所需軟件列表:java

1.eclipse luna 64bit, 保證其中已經自帶了Maven,不然須要經過eclipse的install new software 去安裝一下web

2.jdk 1.7 64bitspring

下面開始第一個demoapache

1.首先手動建立如下目錄及文件瀏覽器

目錄:WorkSpace->myFirstProject->src->main->java->com->example->myFirstProjectapp

文件:WorkSpace\myFirstProject\pom.xml 文件內容以下:eclipse

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.example</groupId>
 7     <artifactId>myFirstproject</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <!-- Inherit defaults from Spring Boot -->
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.4.0.BUILD-SNAPSHOT</version>
15     </parent>
16 
17     <!-- Add typical dependencies for a web application -->
18     <dependencies>
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-web</artifactId>
22         </dependency>
23     </dependencies>
24 
25     <!-- Package as an executable jar -->
26     <build>
27         <plugins>
28             <plugin>
29                 <groupId>org.springframework.boot</groupId>
30                 <artifactId>spring-boot-maven-plugin</artifactId>
31             </plugin>
32         </plugins>
33     </build>
34 
35     <!-- Add Spring repositories -->
36     <!-- (you don't need this if you are using a .RELEASE version) -->
37     <repositories>
38         <repository>
39             <id>spring-snapshots</id>
40             <url>http://repo.spring.io/snapshot</url>
41             <snapshots><enabled>true</enabled></snapshots>
42         </repository>
43         <repository>
44             <id>spring-milestones</id>
45             <url>http://repo.spring.io/milestone</url>
46         </repository>
47     </repositories>
48     <pluginRepositories>
49         <pluginRepository>
50             <id>spring-snapshots</id>
51             <url>http://repo.spring.io/snapshot</url>
52         </pluginRepository>
53         <pluginRepository>
54             <id>spring-milestones</id>
55             <url>http://repo.spring.io/milestone</url>
56         </pluginRepository>
57     </pluginRepositories>
58 </project>
View Code

 

另外新建兩個java文件,maven

WorkSpace\myFirstProject\src\main\java\com\example\myFirstProject\Application.java , 代碼以下,ide

 1 package main.java.com.example.myFirstProject;
 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 
13 }
View Code

 

WorkSpace\myFirstProject\src\main\java\com\example\myFirstProject\Example.java, 代碼以下,spring-boot

 1 package main.java.com.example.myFirstProject;
 2 
 3 
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 @EnableAutoConfiguration
11 public class Example {
12     
13     @RequestMapping("/")
14     String home() {
15         return "Hello World!";
16     }
17     
18     @RequestMapping("/hello/{myName}")
19     String index(@PathVariable String myName) {
20         return "Hello "+myName+"!!!";
21     }
22 }
View Code

 

2.Eclipse配置

File->import->Existing Maven Projects, 定位到WorkSpace\myFirstProject\,能自動讀取pom.xml文件爲Maven項目文件,

導入工程後須要配置build path

2.1 在build path的libraries中,Add library->JRE System Library->WorkSpace default JRE(1.7), 確保將JDK1.7導入工程

2.2 Add library->Maven Managed Dependencies, 以後配置「 Maven Project settings" 輸入Managed Dependencies 便可

最終配置結果以下,

 

3.執行Application.java,運行結果以下,

4.在瀏覽器中輸入 http://localhost:8080, 結果以下,

 

5.在瀏覽器中輸入 http://localhost:8080/hello/world ,結果以下,

 

相關文章
相關標籤/搜索