REST with Spring Boot 2
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Path variables
REST 的參數還可以透過 URL 來傳遞,例如 /api/{var1}/{var2}
,的情況,我們前往 /api/hello/world
的話,var1 就會是 hello,而 var2 則是 world。
創建方法和 GET 非常類似。
- method 前面的
@RequestMapping
中把 URL 的部分加上變數名稱並用{}
框起來- 簡單的做法就是
/api/{var1}
- 也可以用 regular expression 來做 match,例如
/api/{var1:[a-z]+}
,這樣/api/hello
會 match,/api/Hello
就不會進這個 REST API
- 簡單的做法就是
- method 的參數加上
@PathVariable
,變數名稱預設名字和變數名稱要一樣,如果不同的話要特別寫出來- 承 1. 的範例,我們可以寫
@PathVariable String var1
,如果要換名字的話就要寫成@PathVariable("var1") String name
- 承 1. 的範例,我們可以寫
@RestController
public class ExampleController07 {
@RequestMapping(
value = "/example07/{name}/{text:[a-zA-Z0-9]+}",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET
)
public RestQuiz01Response example07(@PathVariable String name, @PathVariable("text") String content) {
RestQuiz01Response result = new RestQuiz01Response();
result.setAnswer(name + ": " + content);
return result;
}
}
下面的練習,請試著根據 TODO 的資訊完成對應的 path variables 設定。
Complete the path variables controller.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.training.rest;
import com.example.training.to.RestQuiz01Response;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class QuizController07 {
/* TODO: update value to accept "/restQuiz07/{name}-{text}" format.
name format is a-z only, without upper cases and numbers.
text can contains any characters
return RestQuiz01Response with answer "{name} says {text}"
*/
@RequestMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public RestQuiz01Response restQuiz07() {
return null;
}
}
Enter to Rename, Shift+Enter to Preview
1
package com.example.training.to;
Enter to Rename, Shift+Enter to Preview
Suggested playgrounds
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content