Java笔记-构造RESTful的WebService
Spring Boot提供了企業級構建RESTful的webService應用
Maven添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>整個porn.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.tutorialspoint</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> </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-web</artifactId></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>?
?
Rest Mapping
@RequestMapping注解定義了請求的URL去訪問REST端。定義消費者好生產者對象。這個注解默認的方法為GET
@RequestMapping(value = "/products") public ResponseEntity<Object> getProducts() { }?
Request Body
@RequestBody注解用于定義請求體的類型
public ResponseEntity<Object> createProduct(@RequestBody Product product) { }?
Path Variable
@PathVariable注解提定義了定制化的或動態的URL請求。
public ResponseEntity<Object> updateProduct(@PathVariable("id") String id) { }?
Request Parameter
@Request Parameter注解回去讀取request url中的參數。其中value是必須的。
public ResponseEntity<Object> getProduct(@RequestParam(value = "name", required = false, defaultValue = "honey") String name) { }?
Get API
HTTP默認的請求就是GET,這個方法不需要HTTP的body。可以通過改變參數和路徑發送不同的URL。
下面是一個簡單的HTTP GET請求方法。使用HashMap存儲生產者。這里使用POJO類進行存儲。
下面的URL中包含/products并且將返回商品列表。下面的controller類包含了REST端的GET方法。
package com.tutorialspoint.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.tutorialspoint.demo.model.Product;@RestController public class ProductServiceController {private static Map<String, Product> 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<Object> getProduct() {return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);} }?
POST API
HTTP的POST方法用于創建資源。這個方法包含了請求的body。可以發送不同的url或者變量。
package com.tutorialspoint.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.tutorialspoint.demo.model.Product;@RestController public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products", method = RequestMethod.POST)public ResponseEntity<Object> createProduct(@RequestBody Product product) {productRepo.put(product.getId(), product);return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED);} }?
PUT API
更新一個存在的資源。此方法包含了Body。
URL請求/products{id},將ID更新到HashMap的Repository中。代碼如下:
package com.tutorialspoint.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.tutorialspoint.demo.model.Product;@RestController public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)public ResponseEntity<Object> 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);} }?
DELETE API
刪除存在的資源。可以添加請求Body。
package com.tutorialspoint.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.tutorialspoint.demo.model.Product;@RestController public class ProductServiceController {private static Map<String, Product> productRepo = new HashMap<>();@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)public ResponseEntity<Object> delete(@PathVariable("id") String id) { productRepo.remove(id);return new ResponseEntity<>("Product is deleted successsfully", HttpStatus.OK);} }下面是Spring Boot啟動類
package com.tutorialspoint.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }下面是POJO類
package com.tutorialspoint.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;} }下面是ProductServiceController.java
package com.tutorialspoint.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.tutorialspoint.demo.model.Product;@RestController public class ProductServiceController {private static Map<String, Product> 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<Object> 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<Object> 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<Object> createProduct(@RequestBody Product product) {productRepo.put(product.getId(), product);return new ResponseEntity<>("Product is created successfully", HttpStatus.CREATED);}@RequestMapping(value = "/products")public ResponseEntity<Object> getProduct() {return new ResponseEntity<>(productRepo.values(), HttpStatus.OK);} }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Java笔记-构造RESTful的WebService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTTP笔记-浏览器是如何识别点击的链接
- 下一篇: Java笔记-解决Required lo