iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 18
2
Modern Web

30天學習Spring MVC系列 第 18

Day 18 Spring Boot RESTful Web Service (上)

前言

這篇偏向於RESTful Web Service的簡單地介紹以及如何於Spring架構中建構RESTful Web Service
如果你想更深入的了解RESTful Web Service如何設計的話我建議要去找些相關書局或到GitHub上去尋找著
適合你的設計模式,在上下章將會來做一個REST Web Service來完成一個CRUD範例

什麼是RESTful Web Service(Representational state transfer)

REST是一個設計理念, REST把所有WEB上的東西都以一個資源(Resource)去看待,並且所有資源都會有一個URI(Uniform Resource Identifier),RESTful Web Service,符合REST Web服務允許系統使用統一和預定的無狀態操作(Representational state transfer)集訪問和操作Web資源

Ex:
我們以Google的API當例子,當你訪問了以下URL將得到什麼樣的結果?
https://maps.googleapis.com/maps/api/geocode/json?address=taipei&sensor=false

得到的結果為以下圖示:
https://ithelp.ithome.com.tw/upload/images/20180106/20107812VhmiILXroS.png

我們訪問得到的結果得到了台北地圖的相關數據並以JSON格式回傳到我們的Client端,你可以試著將json改成xml
格式的話得到的將會是xml格式的數據格式

RESUful只是一種設計風格不是標準

這裡指的標準是協定的標準,REST只是種設計風格

1.所有的API或是以Resource的形式存在。

2.這個服務可以接受與返回某個MIME-TYPE,最常見的是JSON格式,也可以回傳PNG/JPG/TXT等格式。

3.對資源的操作會支援HTTP請求方法 (例如GET, POST, PUT, DELETE)

RESTful風格

RESUful傳輸架構風格最重要的架構約束有6個:

  • 客戶-伺服器(Client-Server
  • 無狀態機制(Stateless
  • 快取(Cache
  • 統一介面(Uniform Interface
  • 分層系統(Layered System
  • 按需代碼(Code-On-Demand,可選)

RESTful設計風格的優點

  • 利用快取來提升回應速度
  • 伺服器可以處理不同的數據返回請求,提高伺服器的擴充功能性
  • 瀏覽器即可作為用戶端,ex:我不需要寫一個client的軟體來讓client去做連線
  • 相對於其他疊加在HTTP協定之上的機制,REST的軟體相依性更小
  • 不需要額外的資源發現機制
  • 易維護,擴展性好,串接服務容易

如何在Spring框架中建構RESTful Web Service?

1.確認你的pom.xml的依賴要有spring-boot-starter-web

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

2.建立MemberAccount Model數據模型

package com.tutorial.Model;

import org.springframework.stereotype.Component;

@Component
public class MemberAccount{

  private int id;
  private String email;
  private String cellphone;
  private String password;
  private String address;
  
  public int getId() {
	return id;
  }
  
  public void setId(int id) {
	this.id = id;
  }
  public String getEmail() {
	return email;
  }
  
  public void setEmail(String email) {
	this.email = email;
  }
  
  public String getCellphone() {
	return cellphone;
  }
  
  public void setCellphone(String cellphone) {
	this.cellphone = cellphone;
  }
  
  public String getPassword() {
	return password;
  }
  
  public void setPassword(String password) {
	this.password = password;
  }
  
  public String getAddress() {
	return address;
  }
  
  public void setAddress(String address) {
	this.address = address;
  }
  
  
  
}


3.建立一個RestController:MemberApiController.java

package com.tutorial.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tutorial.Model.MemberAccount;

@RestController
public class MemberApiController {
	@Autowired
	MemberAccount memberAccount;
	
	 @RequestMapping("/memberApi/memberTest")
	 public MemberAccount memberTest() {
		 MemberAccount memberAccount = new MemberAccount();
		 memberAccount.setAddress("台北市");
		 memberAccount.setCellphone("09123456789");
		 memberAccount.setEmail("test@gmail.com");
		 memberAccount.setId(1);
		 memberAccount.setPassword("123456789");
		 return memberAccount;
	 }
}

完成後示意圖

https://ithelp.ithome.com.tw/upload/images/20180106/20107812nuNSZ9N1Fp.png

參考資料

1.(https://en.wikipedia.org/wiki/Representational_state_transfer ) wiki
2.(https://goo.gl/VyxBbf )
3.(https://spring.io/guides/gs/rest-service/ )

重點介紹

1.我們打造了一個RESTful控制器,但是這樣並非是個RESTful Web Service,要按照RESTful風格來撰寫才是真正的RESTful Web Service
2.第一步要先確認我們的API入口是否能進入


上一篇
Day 17 Spring Boot-檔案上傳篇
下一篇
Day 19 Spring Boot RESTful Web Service (下)
系列文
30天學習Spring MVC30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言