在本教程中,咱們將使用適當的 URI 和 HTTP 方法建立三個服務:php
@GetMapping(「/ students / {studentId} / courses」):您能夠使用請求方法 Get 和示例 uri / students / Student1 / courses 來查詢特定學生已註冊的課程。java
@GetMapping(「/students/{studentId}/courses/{courseId}」):您能夠使用請求方法 Get 和示例 uri / students / Student1 / courses / Course1 獲取特定學生的特定課程。git
@PostMapping(「/students/{studentId}/courses」) :您能夠經過向 UURI /students/Student1/courses 發送 POST 請求來爲學生註冊一門課程github
咱們的 Github 存儲庫包含全部代碼示例 - github.com/in28minutes…web
REST 表明 REpresentational State Transfer。REST 指定了一組體系結構約束。任何知足如下這些條件的服務都稱爲 RESTful 服務。spring
RESTful Web Service 的五個重要條件:typescript
Richardson 成熟度模型用於識別 Restful Web Service 的成熟度級別。如下是不一樣級別和特色:數據庫
級別 0:以 REST 風格公開 SOAP Web 服務。公開的操做使用 REST 服務(http:// server / getPosts,http:// server / deletePosts,http:// server / doThis,http:// server / doThat 等)。apache
級別 1:使用正確的 URI(使用名詞)公開資源。例如:http:// server / accounts,http:// server / accounts / 10。可是,HTTP 方法並未使用。json
級別 2:資源使用正確的 URI + HTTP 方法。例如,要更新一個帳戶,你須要作一個 PUT。建立一個賬戶,你作一個 POST。Uri 看起來像 posts/1/comments/5 和 accounts/1/friends/1.
等級 3:HATEOAS (Hypermedia as the engine of application state)。您不只能夠了解所請求的信息,還能夠了解服務消費者能夠採起的下一個可能的操做。當請求有關 Facebook 用戶的信息時,REST 服務能夠返回用戶詳細信息以及有關如何獲取他最近的帖子,如何獲取他最近的評論以及如何檢索他朋友的列表的信息。
始終使用 HTTP 方法。有關每種 HTTP 方法的最佳作法以下所述:
GET:不該該更新任何東西。應該是冪等的(屢次調用相同的結果)。可能的返回碼 200(OK)+ 404(NOT FOUND)+400(BAD REQUEST)
POST:應該建立新的資源。理想狀況下返回 JSON 和連接到新建立的資源。儘量使用相同的返回碼。另外:返回碼 201(建立)是可能的。
PUT:更新已知資源。例如:更新客戶詳細信息。可能的返回碼:200(OK)
DELETE:用於刪除資源。
如下屏幕截圖顯示了咱們將建立的項目的結構。
一些細節:
在此我向你們推薦一個架構學習交流羣。交流學習羣號:575745314 裏面會分享一些資深架構師錄製的視頻錄像:有Spring,MyBatis,Netty源碼分析,高併發、高性能、分佈式、微服務架構的原理,JVM性能優化、分佈式架構等這些成爲架構師必備的知識體系。還能領取免費的學習資源,目前受益良多
用 Spring Initializr 建立一個 REST 服務是很是的容易小菜一碟。咱們將使用 Spring Web MVC 做爲咱們的 web 層框架。
Spring Initializr start.spring.io/ 是引導建立 Spri… Boot 項目的好工具。
如上圖所示,必須執行如下步驟
全部應用都須要數據。咱們將使用 ArrayList 這種內存數據存儲,而不是與真實數據庫交互。
一名學生能夠參加多門課程。課程有一個 ID,名稱,說明和完成課程須要完成的步驟列表。學生有一個身份證,姓名,說明和他 / 她目前註冊的課程列表。StudentService 提供如下公開方法
請參閱下面這些文件,具體的實現服務類 StudentService 和模型類 Course 和 Student。
Rest 服務 StudentController 暴露了幾個 get 服務。
package com.in28minutes.springboot.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.service.StudentService;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students/{studentId}/courses")
public List<Course> retrieveCoursesForStudent(@PathVariable String studentId) {
return studentService.retrieveCourses(studentId);
}
@GetMapping("/students/{studentId}/courses/{courseId}")
public Course retrieveDetailsForCourse(@PathVariable String studentId,
@PathVariable String courseId) {
return studentService.retrieveCourse(studentId, courseId);
}
}
複製代碼
使用 Postman 執行獲取服務
咱們將向 http:// localhost:8080 / students / Student1 / courses / Course1 發起請求以測試該服務。迴應以下所示。
{
"id": "Course1",
"name": "Spring",
"description": "10 Steps",
"steps": [
"Learn Maven",
"Import Project",
"First Example",
"Second Example"
]
}
複製代碼
下面的圖片顯示了咱們如何執行 Postman 的 Get Service - 我最喜歡的運行 rest 服務的工具。
當資源建立成功時,POST 服務應該返回建立的狀態(201)。
@PostMapping("/students/{studentId}/courses")
public ResponseEntity<Void> registerStudentForCourse(
@PathVariable String studentId, @RequestBody Course newCourse) {
Course course = studentService.addCourse(studentId, newCourse);
if (course == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(
"/{id}").buildAndExpand(course.getId()).toUri();
return ResponseEntity.created(location).build();
}
複製代碼
執行 POST Rest 服務
示例請求以下所示。它包含了學生註冊課程的全部細節。
{
"name": "Microservices",
"description": "10 Steps",
"steps": [
"Learn How to Break Things Up",
"Automate the hell out of everything",
"Have fun"
]
}
複製代碼
下圖顯示了咱們如何從 Postman 執行 Post 服務 - 我最喜歡的運行 rest 服務的工具。確保你去 Body 選項卡並選擇 raw。從下拉菜單中選擇 JSON。將上述請求複製到 body 中。
咱們使用的 URL 是 http:// localhost:8080 / students / Student1 / courses。
在此我向你們推薦一個架構學習交流羣。交流學習羣號:575745314 裏面會分享一些資深架構師錄製的視頻錄像:有Spring,MyBatis,Netty源碼分析,高併發、高性能、分佈式、微服務架構的原理,JVM性能優化、分佈式架構等這些成爲架構師必備的知識體系。還能領取免費的學習資源,目前受益良多
pom.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.in28minutes.springboot</groupId>
<artifactId>student-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>student-services</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
複製代碼
import java.net.URI;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.service.StudentService;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/students/{studentId}/courses")
public List<Course> retrieveCoursesForStudent(@PathVariable String studentId) {
return studentService.retrieveCourses(studentId);
}
@GetMapping("/students/{studentId}/courses/{courseId}")
public Course retrieveDetailsForCourse(@PathVariable String studentId,
@PathVariable String courseId) {
return studentService.retrieveCourse(studentId, courseId);
}
@PostMapping("/students/{studentId}/courses")
public ResponseEntity<Void> registerStudentForCourse(
@PathVariable String studentId, @RequestBody Course newCourse) {
Course course = studentService.addCourse(studentId, newCourse);
if (course == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(
"/{id}").buildAndExpand(course.getId()).toUri();
return ResponseEntity.created(location).build();
}
}
複製代碼
import java.util.List;
public class Course {
private String id;
private String name;
private String description;
private List<String> steps;
// Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException:
// Can not construct instance of com.in28minutes.springboot.model.Course:
// no suitable constructor found, can not deserialize from Object value
// (missing default constructor or creator, or perhaps need to add/enable
// type information?)
public Course() {
}
public Course(String id, String name, String description, List<String> steps) {
super();
this.id = id;
this.name = name;
this.description = description;
this.steps = steps;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public List<String> getSteps() {
return steps;
}
@Override
public String toString() {
return String.format(
"Course [id=%s, name=%s, description=%s, steps=%s]", id, name,
description, steps);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Course other = (Course) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
複製代碼
package com.in28minutes.springboot.model;
import java.util.List;
public class Student {
private String id;
private String name;
private String description;
private List<Course> courses;
public Student(String id, String name, String description,
List<Course> courses) {
super();
this.id = id;
this.name = name;
this.description = description;
this.courses = courses;
}
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;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return String.format(
"Student [id=%s, name=%s, description=%s, courses=%s]", id,
name, description, courses);
}
}
複製代碼
package com.in28minutes.springboot.service;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.in28minutes.springboot.model.Course;
import com.in28minutes.springboot.model.Student;
@Component
public class StudentService {
private static List<Student> students = new ArrayList<>();
static {
//Initialize Data
Course course1 = new Course("Course1", "Spring", "10 Steps", Arrays
.asList("Learn Maven", "Import Project", "First Example",
"Second Example"));
Course course2 = new Course("Course2", "Spring MVC", "10 Examples",
Arrays.asList("Learn Maven", "Import Project", "First Example",
"Second Example"));
Course course3 = new Course("Course3", "Spring Boot", "6K Students",
Arrays.asList("Learn Maven", "Learn Spring",
"Learn Spring MVC", "First Example", "Second Example"));
Course course4 = new Course("Course4", "Maven",
"Most popular maven course on internet!", Arrays.asList(
"Pom.xml", "Build Life Cycle", "Parent POM",
"Importing into Eclipse"));
Student ranga = new Student("Student1", "Ranga Karanam",
"Hiker, Programmer and Architect", new ArrayList<>(Arrays
.asList(course1, course2, course3, course4)));
Student satish = new Student("Student2", "Satish T",
"Hiker, Programmer and Architect", new ArrayList<>(Arrays
.asList(course1, course2, course3, course4)));
students.add(ranga);
students.add(satish);
}
public List<Student> retrieveAllStudents() {
return students;
}
public Student retrieveStudent(String studentId) {
for (Student student : students) {
if (student.getId().equals(studentId)) {
return student;
}
}
return null;
}
public List<Course> retrieveCourses(String studentId) {
Student student = retrieveStudent(studentId);
if (student == null) {
return null;
}
return student.getCourses();
}
public Course retrieveCourse(String studentId, String courseId) {
Student student = retrieveStudent(studentId);
if (student == null) {
return null;
}
for (Course course : student.getCourses()) {
if (course.getId().equals(courseId)) {
return course;
}
}
return null;
}
private SecureRandom random = new SecureRandom();
public Course addCourse(String studentId, Course course) {
Student student = retrieveStudent(studentId);
if (student == null) {
return null;
}
String randomId = new BigInteger(130, random).toString(32);
course.setId(randomId);
student.getCourses().add(course);
return course;
}
}
複製代碼
package com.in28minutes.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudentServicesApplication {
public static void main(String[] args) {
SpringApplication.run(StudentServicesApplication.class, args);
}
}複製代碼