REST

REST (REpresentational State Transfer)

2021/11/28 (增加連結)

基本概念

REST (REpresentational State Transfer)是提供web services的一種方法,REST利用HTTP的POST,GET , PUT, DELETE來進行CRUD。

  • True RESTful API : REST Architectural Constraints

    • Stateless

    • Client-Server

    • Uniform Interface

    • Cacheable

    • Layered system

    • Code on demand

  • 11 things you should know about GET vs POST

    • GET parameters will be stored on browser’s session history.

    • GET responses can be cached

    • Get is a safe method

    • Static Websites only responds to GET requests

    • Which method has length restrictions? GET

    • Which method is more secure and should be used to deal with sensitive data? POST

    • Which method can be bookmarked? GET

一般而言,我們是利用RESTful web services,與其他的web site或手機交換資料。所以,下面的範例可以想像成去另外一個網站(雖然目前的url是localhost)取得資料。

http://localhost:8080/customer/1 的內容類似:

{

"name" : "Ben",

"address" : "新北市新莊區中正路510號",

"weight" : 60,

"_links" : {

"self" : {

"href" : "http://localhost:8080/customer/1"

},

"customer" : {

"href" : "http://localhost:8080/customer/1"

}

}

}

http://localhost:8080/customer 的內容類似:

{

"_embedded" : {

"customer" : [ {

"name" : "Ben",

"address" : "新北市新莊區中正路510號",

"weight" : 60,

"_links" : {

"self" : {

"href" : "http://localhost:8080/customer/1"

},

"customer" : {

"href" : "http://localhost:8080/customer/1"

}

}

}, {

"name" : "Cathy",

"address" : "台北市仁愛路1號",

"weight" : 65,

"_links" : {

"self" : {

"href" : "http://localhost:8080/customer/2"

},

"customer" : {

"href" : "http://localhost:8080/customer/2"

}

}

} ]

},

"_links" : {

"self" : {

"href" : "http://localhost:8080/customer{?page,size,sort}",

"templated" : true

},

"profile" : {

"href" : "http://localhost:8080/profile/customer"

}

},

"page" : {

"size" : 20,

"totalElements" : 2,

"totalPages" : 1,

"number" : 0

}

}

Spring REST

基本上,在Spring 4裡,加上了@RestController就可以! (詳參: Spring MVC @RestController與@Controller的區別)

package com.example.demo;


import java.util.ArrayList;

import java.util.List;

import org.springframework.boot.SpringApplication;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;


@RestController

public class EmployeeController {

private List<Employee> employeeList = new ArrayList<>();

private int count=0;

public EmployeeController(){

employeeList.add(new Employee(1, "Arpit", "IT"));

employeeList.add(new Employee(2, "Sanjeev", "IT"));

employeeList.add(new Employee(3, "Ben", "IT"));

count = 3;

}


@GetMapping("/employee/get")

public List<Employee> get() {

return employeeList;

}

@PostMapping("/employee/")

public void post(@RequestBody Employee employee) {

count++;

employee.setId(count);

employeeList.add(employee);

}

}

輸入

http://localhost:8080/employee/get

會得到

[{"id":1,"name":"Arpit","department":"IT"},{"id":2,"name":"Sanjeev","department":"IT"},{"id":3,"name":"Ben","department":"IT"}]

Data REST

除了RestController,Spring還提供Spring Data Rest,基本上Spring Data Rest是將Rest架在Spring JPA上,也就是當資料的存取設定好了之後,REST服務就好了,提供REST服務,可以讓App (不管是Android或iOS)都可以新增、取得、更新及刪除資料,詳參: Spring REST (Spring Data Rest)

客製搜尋

If you have query methods defined, those also default to be exposed by their name:

interface PersonRepository extends CrudRepository<Person, Long> {


List<Person> findByName(String name);

}

This would be exposed under the URL: http://localhost:8080/persons/search/findByName

All query method resources are exposed under the resource search.

To change the segment of the URL under which this query method is exposed, use the @RestResource annotation again:

@RepositoryRestResource(path = "people")

interface PersonRepository extends CrudRepository<Person, Long> {


@RestResource(path = "names")

List<Person> findByName(String name);

}

Now this query method will be exposed under the URL: http://localhost:8080/people/search/names

詳參: https://docs.spring.io/spring-data/rest/docs/current/reference/html/

參考資料

REST

Spring

JAX-RS

Firebase