Spring官網的Guides部分,提供了數十個簡短的學習用例,個人感覺是非常好的學習資源,今天的學習就是該Guides的第一個例子:開發一個簡單的RESTful Web Service。該例子的英文頁面爲:https://spring.io/guides/gs/rest-service/。
- 使用Spring Initializr構建pom.xml文件
通常,構建Spring應用程序的第一步都是使用spring initializr構建一個描述項目的pom.xml文件,然後就可以使用maven來管理該開發項目。Spring initializr是spring官方提供的一個在線工具,其地址爲:https://start.spring.io/。
打開initializr頁面後
Project項選擇Maven Project;
Language項選擇Java;
Spring Boot項選擇你希望的spring版本,我通常選擇正式release的版本,就是不帶M1和SNAPSHOT標誌的版本。
Group項可以填開發單位的信息,我是個人,所以我填我名字的拼音com.boliang。
Artifact項可以填項目的名稱,我填restservice。
Dependencies項爲該項目依賴的其他組件,這裏我們只依賴於Spring Web
最後點擊"Explore - Ctrl + Space"按鈕,可以查看在線生成的pom.xml文件,之後可以選擇將該文件下載到本地的項目文件夾。 - 在項目的src/main/java/目錄下創建Greeting.java文件,內容如下:
package com.boliang.restservice; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }
- 在項目的src/main/java/目錄下創建GreetingController.java文件,內容如下:
package com.boliang.restservice; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @GetMapping("/greeting") public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } }
@RestController 註釋表示GreetingController 類是一個控制器
@GetMapping 註釋表示greeting方法對應到對/greeting URI的GET方法上
@RequestParam 註釋表示將http請求中的name參數的是值對應到greeting方法的name參數。 - 在項目src/main/java/目錄下創建RestService.java,內容爲如下:
package com.boliang.restservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestService { public static void main(String[] args) { SpringApplication.run(RestService .class, args); } }
- 運行
在pom.xml所在的項目目錄下運行以下命令:mvn spring-boot:run
接下來你會看到maven自動下載一大堆東西,這個過程可能會比較久,耐心等待一會,你會看到spring運行的界面:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.4.RELEASE) 2020-02-07 21:47:58.269 INFO 15088 --- [ main] com.boliang.restservice.RestService : Starting RestService on DESKTOP-N0EF3ON with PID 15088 (C:\Users\bolia\eclipse-workspace\rest-service\target\classes started by bolia in C:\Users\bolia\eclipse-workspace\rest-service) 2020-02-07 21:47:58.272 INFO 15088 --- [ main] com.boliang.restservice.RestService : No active profile set, falling back to default profiles: default 2020-02-07 21:47:59.403 INFO 15088 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-02-07 21:47:59.415 INFO 15088 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-02-07 21:47:59.419 INFO 15088 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30] 2020-02-07 21:47:59.531 INFO 15088 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-02-07 21:47:59.533 INFO 15088 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1192 ms 2020-02-07 21:47:59.712 INFO 15088 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-02-07 21:47:59.890 INFO 15088 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-02-07 21:47:59.898 INFO 15088 --- [ main] com.boliang.restservice.RestService : Started RestService in 2.071 seconds (JVM running for 2.526)
-
訪問服務
接下來打開瀏覽器,在地址欄輸入:http://127.0.0.1:8080/greeting 將可以看到:
{"id":1,"content":"Hello, World!"}
我們還可以加上一個參數,在地址欄輸入:http://127.0.0.1:8080/greeting?name=liangbo 將可以看到:
{"id":1,"content":"Hello, liangbo!"} -
結束程序可以按Ctrl+C
-
打包
在項目根目錄輸入:mvn package
命令執行完成以後,在target目錄下會生成 restservice-0.0.1-SNAPSHOT.jar,文件大概在10多兆,這是打包了所有依賴庫的包,可以直接在java環境中運行。
-
運行jar
在項目根目錄輸入:java -jar target/restservice-0.0.1-SNAPSHOT.jar
然後就可以看到類似第5步的運行界面。
至此,此例完成。