SpringBoot實戰(一)Hello World

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。

1.首先介紹極簡的helloworld工程

1).打開Eclipse,新建Maven工程,修改pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.enn</groupId>
	<artifactId>SpringBoot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-sample-mysql</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2)、編制Application.java存於myFirstProject\src\main\java\com\example\myFirstProject下

[java] view plain copy
  1. package com.example.myFirstProject;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6.   
  7. @SpringBootApplication  
  8. public class Application {  
  9.   
  10.     public static void main(String[] args) {  
  11.         SpringApplication.run(Application.class, args);  
  12.     }  
  13. }  
3)、編制Example.java,存於myFirstProject\src\main\java\com\example\myFirstProject下

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

4).再次點擊“OK”按鈕,在Eclipse的Console中開始打印如圖5


5)、打開瀏覽器,輸入http://localhost:8080,顯示如圖6

6)、在瀏覽器中,輸入http://localhost:8080/hello/SpringBoot

2、使用官網樣例創建工程

一、在瀏覽器中打開http://start.spring.io/,如圖

Artifact中輸入spring-boot-sample-helloworld,點擊“Switch to the full version.”,勾選"web",然後點擊“ Generate Project alt +”按鈕,把文件保存到本地某個位置

二、下載文件導入eclips

1、解壓下載的文件到某個文件夾;

2、在eclips中導入工程file->import->Import Existing Maven Projects-->Select Maven projects-->finish

3、在eclips中運行工程,正確則入圖

三、添加HelloController

1、在包com.example中點擊右鍵,選擇new->class,再在Name中輸入HelloController

2、在類上面條件聲明@RestController

3、增加方法

[java] view plain copy
  1. @RequestMapping("/")  
  2.     public String helloworld(){  
  3.         return "Hello world!";  
  4.     }  
  5.       
  6.     @RequestMapping("/hello/{name}")  
  7.     public String hellName(@PathVariable String Name){  
  8.         return "Hello "+Name;  
  9.     }  

4、保存Ctrl+S

5、自動添加引用 Ctrl+SHift+O

6、整個HelloController文件如下

[java] view plain copy
  1. package com.example;  
  2.   
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class HelloController {  
  9.   
  10.     @RequestMapping("/")  
  11.     public String helloworld(){  
  12.         return "Hello world!";  
  13.     }  
  14.       
  15.     @RequestMapping("/hello/{name}")  
  16.     public String hellName(@PathVariable String name){  
  17.         return "Hello "+name;  
  18.     }  
  19. }  
7、啓動測試

在瀏覽器中依次輸入

http://localhost:8080/

http://localhost:8080/hello/上帝




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章