元宇宙iwemeta:Spring Boot構建RESTful Web服務

Spring Boot為為企業應用程式構建RESTful Web服務提供了非常好的支援。 本章將詳細介紹如何使用Spring Boot構建RESTful Web服務。

如果是Maven使用者,請使用以下程式碼在

pom。xml

檔案中新增以下依賴項 -

org。springframework。boot spring-boot-starter-web XML

如果是Gradle使用者,請使用以下程式碼在

build。gradle

檔案中新增以下依賴項。

compile(‘org。springframework。boot:spring-boot-starter-web’)Shell

完整的構建配置檔案Maven build -

pom。xml

的程式碼如下 -

<?xml version = “1。0” encoding = “UTF-8”?> 4。0。0 com。yiibai demo 0。0。1-SNAPSHOT jar demo Demo project for Spring Boot org。springframework。boot spring-boot-starter-parent 1。5。8。RELEASE UTF-8 UTF-8 1。8 org。springframework。boot spring-boot-starter-web org。springframework。boot spring-boot-starter-test test org。springframework。boot spring-boot-maven-plugin XML

完整的構建配置檔案Gradle Build -

build。gradle

的程式碼如下 -

buildscript { ext { springBootVersion = ‘1。5。8。RELEASE’ } repositories { mavenCentral() } dependencies { classpath(“org。springframework。boot:spring-boot-gradle-plugin:${springBootVersion}”) }}apply plugin: ‘java’apply plugin: ‘eclipse’apply plugin: ‘org。springframework。boot’group = ‘com。yiibai’version = ‘0。0。1-SNAPSHOT’sourceCompatibility = 1。8repositories { mavenCentral()}dependencies { compile(‘org。springframework。boot:spring-boot-starter-web’) testCompile(‘org。springframework。boot:spring-boot-starter-test’)}Gradle

在繼續構建RESTful Web服務之前,建議瞭解以下注釋 -

Rest控制器

@RestController註釋用於定義RESTful Web服務。它提供JSON,XML和自定義響應。其語法如下所示 -

@RestControllerpublic class ProductServiceController {}Java

請求對映

@RequestMapping註釋用於定義訪問REST端點的Request URI。可以定義Request方法來使用和生成物件。預設請求方法是:GET。

@RequestMapping(value = “/products”)public ResponseEntity getProducts() { }Java

請求主體

@RequestBody註釋用於定義請求正文內容型別。

public ResponseEntity createProduct(@RequestBody Product product) {}Java

路徑變數

@PathVariable批註用於定義自定義或動態請求URI。 請求URI中的Path變數定義為花括號{},如下所示 -

public ResponseEntity updateProduct(@PathVariable(“id”) String id) {}Java

請求引數

@RequestParam註釋用於從請求URL讀取請求引數。預設情況下,它是必需引數。還可以為請求引數設定預設值,如下所示 -

public ResponseEntity getProduct( @RequestParam(value = “name”, required = false, defaultValue = “honey”) String name) {}Java

GET API

預設的HTTP請求方法是GET。此方法不需要任何請求主體。可以傳送請求引數和路徑變數來自定義或動態URL。

用於定義HTTP GET請求方法的示例程式碼如下所示。 在此示例中使用HashMap儲存產品。 請注意,使用POJO類作為要儲存的產品。

這裡,請求URI是/products,它將從HashMap儲存庫返回產品列表。下面給出了包含GET方法REST端點的控制器類檔案。

package com。yiibai。demo。controller;import java。util。HashMap;import java。util。Map;import org。springframework。http。HttpStatus;import org。springframework。http。ResponseEntity;import org。springframework。web。bind。annotation。RequestMapping;import org。springframework。web。bind。annotation。RestController;import com。yiibai。demo。model。Product;@RestControllerpublic class ProductServiceController { private static Map productRepo = new HashMap<>(); static { Product honey = new Product(); honey。setId(“1”); honey。setName(“Honey”); productRepo。put(honey。getId(), honey); Product almond = new Product(); almond。setId(“2”); almond。setName(“Almond”); productRepo。put(almond。getId(), almond); } @RequestMapping(value = “/products”) public ResponseEntity getProduct() { return new ResponseEntity<>(productRepo。values(), HttpStatus。OK); }}Java

POST API

HTTP POST請求用於建立資源。 此方法包含請求正文。可以傳送請求引數和路徑變數來定義自定義或動態URL。

以下示例顯示了用於定義HTTP POST請求方法的示例程式碼。 在此示例中,使用HashMap儲存Product,其中產品是POJO類。

這裡,請求URI是/products,它會在將產品儲存到HashMap儲存庫後返回字串。

package com。yiibai。demo。controller;import java。util。HashMap;import java。util。Map;import org。springframework。http。HttpStatus;import org。springframework。http。ResponseEntity;import org。springframework。web。bind。annotation。RequestBody;import org。springframework。web。bind。annotation。RequestMapping;import org。springframework。web。bind。annotation。RequestMethod;import org。springframework。web。bind。annotation。RestController;import com。yiibai。demo。model。Product;@RestControllerpublic class ProductServiceController { private static Map productRepo = new HashMap<>(); @RequestMapping(value = “/products”, method = RequestMethod。POST) public ResponseEntity createProduct(@RequestBody Product product) { productRepo。put(product。getId(), product); return new ResponseEntity<>(“Product is created successfully”, HttpStatus。CREATED); }}Java

PUT API

HTTP PUT請求用於更新現有資源,此方法包含請求正文。可以傳送請求引數和路徑變數來自定義或動態URL。

下面給出的示例顯示瞭如何定義HTTP PUT請求方法。 在此示例中使用HashMap更新現有產品,其中產品是POJO類。

這裡的請求URI是/products/{id},它將產品儲存到HashMap庫後返回String。 請注意,使用路徑變數{id}來定義需要更新的產品ID。

package com。yiibai。demo。controller;import java。util。HashMap;import java。util。Map;import org。springframework。http。HttpStatus;import org。springframework。http。ResponseEntity;import org。springframework。web。bind。annotation。PathVariable;import org。springframework。web。bind。annotation。RequestBody;import org。springframework。web。bind。annotation。RequestMapping;import org。springframework。web。bind。annotation。RequestMethod;import org。springframework。web。bind。annotation。RestController;import com。yiibai。demo。model。Product;@RestControllerpublic class ProductServiceController { private static Map productRepo = new HashMap<>(); @RequestMapping(value = “/products/{id}”, method = RequestMethod。PUT) public ResponseEntity updateProduct(@PathVariable(“id”) String id, @RequestBody Product product) { productRepo。remove(id); product。setId(id); productRepo。put(id, product); return new ResponseEntity<>(“Product is updated successsfully”, HttpStatus。OK); } }Java

DELETE API

HTTP刪除請求用於刪除現有資源。此方法不包含任何請求正文。可以傳送請求引數和路徑變數來自定義或動態URL。

下面給出的示例顯示瞭如何定義HTTP DELETE請求方法。 在此示例中,使用HashMap刪除現有產品,即POJO類。

請求URI是/products/{id},它將在從HashMap儲存庫中刪除產品後返回字串。使用路徑變數{id}來定義需要刪除的產品ID。

package com。yiibai。demo。controller;import java。util。HashMap;import java。util。Map;import org。springframework。http。HttpStatus;import org。springframework。http。ResponseEntity;import org。springframework。web。bind。annotation。PathVariable;import org。springframework。web。bind。annotation。RequestBody;import org。springframework。web。bind。annotation。RequestMapping;import org。springframework。web。bind。annotation。RequestMethod;import org。springframework。web。bind。annotation。RestController;import com。yiibai。demo。model。Product;@RestControllerpublic class ProductServiceController { private static Map productRepo = new HashMap<>(); @RequestMapping(value = “/products/{id}”, method = RequestMethod。DELETE) public ResponseEntity delete(@PathVariable(“id”) String id) { productRepo。remove(id); return new ResponseEntity<>(“Product is deleted successsfully”, HttpStatus。OK); }}Java

本節提供完整的原始碼集。請遵守以下程式碼瞭解其各自的功能 -

Spring Boot主應用程式類 -

DemoApplication。java

package com。yiibai。demo;import org。springframework。boot。SpringApplication;import org。springframework。boot。autoconfigure。SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication。run(DemoApplication。class, args); }}Java

POJO類

- Product。java

package com。yiibai。demo。model;public class Product { private String id; private String name; public String getId() { return id; } public void setId(String id) { this。id = id; } public String getName() { return name; } public void setName(String name) { this。name = name; }}Java

Rest Controller類

- ProductServiceController。java

package com。yiibai。demo。controller;import java。util。HashMap;import java。util。Map;import org。springframework。http。HttpStatus;import org。springframework。http。ResponseEntity;import org。springframework。web。bind。annotation。PathVariable;import org。springframework。web。bind。annotation。RequestBody;import org。springframework。web。bind。annotation。RequestMapping;import org。springframework。web。bind。annotation。RequestMethod;import org。springframework。web。bind。annotation。RestController;import com。yiibai。demo。model。Product;@RestControllerpublic class ProductServiceController { private static Map productRepo = new HashMap<>(); static { Product honey = new Product(); honey。setId(“1”); honey。setName(“Honey”); productRepo。put(honey。getId(), honey); Product almond = new Product(); almond。setId(“2”); almond。setName(“Almond”); productRepo。put(almond。getId(), almond); } @RequestMapping(value = “/products/{id}”, method = RequestMethod。DELETE) public ResponseEntity delete(@PathVariable(“id”) String id) { productRepo。remove(id); return new ResponseEntity<>(“Product is deleted successsfully”, HttpStatus。OK); } @RequestMapping(value = “/products/{id}”, method = RequestMethod。PUT) public ResponseEntity updateProduct(@PathVariable(“id”) String id, @RequestBody Product product) { productRepo。remove(id); product。setId(id); productRepo。put(id, product); return new ResponseEntity<>(“Product is updated successsfully”, HttpStatus。OK); } @RequestMapping(value = “/products”, method = RequestMethod。POST) public ResponseEntity createProduct(@RequestBody Product product) { productRepo。put(product。getId(), product); return new ResponseEntity<>(“Product is created successfully”, HttpStatus。CREATED); } @RequestMapping(value = “/products”) public ResponseEntity getProduct() { return new ResponseEntity<>(productRepo。values(), HttpStatus。OK); }}Java

可以建立一個可執行的JAR檔案,並使用下面的Maven或Gradle命令執行spring boot應用程式。

對於Maven,請使用下面顯示的命令 -

mvn clean installShell

在執行命令之後,看到控制檯輸出“BUILD SUCCESS”之後,在目標目錄下找到JAR檔案。對於Gradle,請使用下面顯示的命令 -

gradle clean buildShell

在看到控制檯輸出“BUILD SUCCESSFUL”之後,可以在build/libs目錄下找到JAR檔案。使用下面顯示的命令執行JAR檔案 -

java –jar Java

這將在Tomcat埠8080上啟動應用程式。

元宇宙iwemeta:Spring Boot構建RESTful Web服務