javascript
Spring Boot:构建一个RESTful Web应用程序
介紹:
REST代表表示狀態(tài)傳輸 ,是API設(shè)計(jì)的體系結(jié)構(gòu)指南。 我們假設(shè)您已經(jīng)具有構(gòu)建RESTful API的背景。
在本教程中,我們將設(shè)計(jì)一個(gè)簡(jiǎn)單的Spring Boot RESTful Web應(yīng)用程序,公開(kāi)一些REST端點(diǎn)。
項(xiàng)目設(shè)置:
讓我們首先通過(guò)Spring Initializr下載項(xiàng)目模板:
對(duì)于RESTful Web應(yīng)用程序,我們只需要添加“ Spring Web”作為額外的入門(mén)依賴。 假設(shè)我們也在與數(shù)據(jù)庫(kù)進(jìn)行交互,則添加了其他兩個(gè)。
現(xiàn)在,我們的POM文件將具有所有需要的Web應(yīng)用程序和數(shù)據(jù)庫(kù)依賴性:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope> </dependency>REST控制器:
現(xiàn)在讓我們定義REST控制器:
@RestController @RequestMapping("/student") public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("/all")public ResponseEntity<List<Student>> getAllStudents() {return new ResponseEntity<List<Student>>(studentService.getAllStudents(), HttpStatus.OK);}@GetMapping("/{id}") public ResponseEntity<Student> getStudentById(@PathVariable("id") Integer id) {Optional<Student> student = studentService.getById(id);if(student.isPresent())return new ResponseEntity<Student>(student.get(), HttpStatus.OK);else throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No student found!"); }@PostMapping("/")public ResponseEntity<Student> createStudent(@RequestBodyStudent student) {Student newStudent = studentService.store(student);return new ResponseEntity<Student>(newStudent, HttpStatus.CREATED);}... }我們可以在控制器中定義所有的GET,POST,DELETE或PUT映射。
服務(wù):
在這里, StudentService是與數(shù)據(jù)庫(kù)交互并為我們執(zhí)行所有操作的類(lèi):
@Service public class StudentService {@Autowiredprivate StudentRepository repo;public Student store(Student student) {return repo.save(student);}public List<Student> getAllStudents() {return repo.findAll();}...}我們還有另一本教程,說(shuō)明如何使用Spring Boot配置H2數(shù)據(jù)庫(kù)。
運(yùn)行應(yīng)用程序:
最后,我們可以運(yùn)行我們的UniversityApplication類(lèi):
@SpringBootApplication public class UniversityApplication {public static void main(String[] args) {SpringApplication.run(UniversityApplication.class, args);} }通過(guò)它,我們的REST端點(diǎn)將在嵌入式服務(wù)器上公開(kāi)。
測(cè)試REST端點(diǎn):
讓我們使用cURL來(lái)測(cè)試我們的REST端點(diǎn):
$ curl http://localhost:8080/student/all這將返回?cái)?shù)據(jù)庫(kù)中存在的所有學(xué)生記錄:
[{1, "James"}, {2, "Selena"}, {3, "John"}]同樣,我們有:
$ curl http://localhost:8080/student/1 {1, "James"}我們還可以使用POSTman工具來(lái)測(cè)試我們的端點(diǎn)。 它具有出色的用戶界面。
結(jié)論:
在本教程中,我們從頭開(kāi)始構(gòu)建了一個(gè)Spring Boot RESTful應(yīng)用程序。 我們公開(kāi)了一些API,然后使用cURL對(duì)其進(jìn)行了測(cè)試。
翻譯自: https://www.javacodegeeks.com/2019/09/spring-boot-building-restful-web-application.html
總結(jié)
以上是生活随笔為你收集整理的Spring Boot:构建一个RESTful Web应用程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑自动开机方案如何设定电脑自动开机
- 下一篇: Spring Boot登录选项快速指南