javascript
Spring Boot 菜鸟教程 1 HelloWorld
GitHub
src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">技能要求
- 最好對Spring有一定認識
- 最好對Maven有一定認識
簡介
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
功能
- 創建獨立的Spring applications
- 能夠使用內嵌的Tomcat, Jetty or Undertow,不需要部署war
- 提供starter pom來簡化maven配置
- 自動配置Spring
- 提供一些生產環境的特性,比如metrics, health checks and externalized configuration
- 絕對沒有代碼生成和XML配置要求
開篇
- 如果你用過Spring JavaConfig的話,會發現雖然沒有了xml配置的繁瑣,但是使用各種注解導入也是很大的坑,
- 然后在使用一下Spring Boot,你會有一縷清風拂過的感覺,
- 最后真是爽的不得了。。。
項目結構圖
核心注解類說明
@RestController
就是@Controller+@ResponseBody組合,支持RESTful訪問方式,返回結果都是json字符串
@SpringBootApplication
就是@SpringBootConfiguration+@EnableAutoConfiguration+
@ComponentScan等組合在一下,非常簡單,使用也方便
@SpringBootTest
Spring Boot版本1.4才出現的,具有Spring Boot支持的引導程序(例如,加載應用程序、屬性,為我們提供Spring Boot的所有精華部分)
關鍵是自動導入測試需要的類。。。
配置文件pom.xml
<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.jege.spring.boot</groupId><artifactId>spring-boot-hello-world</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-hello-world</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依賴jar文件不用在寫版本號 --><parent><groupId>org.springframework.boot</groupId><!-- 自動包含以下信息: --><!-- 1.使用Java6編譯級別 --><!-- 2.使UTF-8編碼 --><!-- 3.實現了通用的測試框架 (JUnit, Hamcrest, Mockito). --><!-- 4.智能資源過濾 --><!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). --><artifactId>spring-boot-starter-parent</artifactId><!-- spring boot 1.x最后穩定版本 --><version>1.4.1.RELEASE</version><!-- 表示父模塊pom的相對路徑,這里沒有值 --><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 測試 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><!-- 只在test測試里面運行 --><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-hello-world</finalName><plugins><!-- jdk編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build> </project>啟動類Application
package com.jege.spring.boot;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author JE哥* @email 1272434821@qq.com* @description:spring boot 啟動類*/@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}控制器HelloWorldController
package com.jege.spring.boot.hello.world;import java.util.Arrays; import java.util.List;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author JE哥* @email 1272434821@qq.com* @description:看看spring-boot的強大和方便*/ @RestController public class HelloWorldController {@RequestMapping("/hello1")public String hello1() {return "Hello World";}@RequestMapping("/hello2")public List<String> hello2() {return Arrays.asList(new String[] { "A", "B", "C" });} }測試類HelloWorldControllerTest
package com.jege.spring.boot.hello.world;import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import org.junit.Before; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;/*** @author JE哥* @email 1272434821@qq.com* @description:以Mock方式測試Controller*/ @SpringBootTest public class HelloWorldControllerTest {private MockMvc mockMvc;@Beforepublic void setUp() throws Exception {mockMvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();}@Testpublic void getHello() throws Exception {mockMvc.perform(MockMvcRequestBuilders.get("/hello1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Hello World")));}@Testpublic void getHello2() throws Exception {mockMvc.perform(MockMvcRequestBuilders.get("/hello2").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("[\"A\",\"B\",\"C\"]")));}}運行
運行Application的main方法,打開瀏覽器:
http://localhost:8080/hello1
輸出Hello World
http://localhost:8080/hello2
輸出[“A”,”B”,”C”]
運行HelloWorldControllerTest
以Mock方式測試Controller
源碼地址
https://github.com/je-ge/spring-boot
如果覺得我的文章或者代碼對您有幫助,可以請我喝杯咖啡。
您的支持將鼓勵我繼續創作!謝謝!
總結
以上是生活随笔為你收集整理的Spring Boot 菜鸟教程 1 HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA Spring环境搭建+简单入门
- 下一篇: Spring 菜鸟教程 异常 集锦