建立 REST 服務

  1. 使用 STS(Spring Starter Project)或 Spring Initializr( https://start.spring.io ) 建立專案。
  2. 在 pom.xml 中新增 Web 依賴項:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

或在 Search for dependencies 搜尋框中鍵入 web ,新增 Web 依賴項並下載壓縮專案。

  1. 建立域類(即使用者)
 public class User {
    
        private Long id;
    
        private String userName;
    
        private String password;
    
        private String email;
    
        private String firstName;
    
        private String lastName;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email
                    + ", firstName=" + firstName + ", lastName=" + lastName + "]";
        }
    
        public User(Long id, String userName, String password, String email, String firstName, String lastName) {
            super();
            this.id = id;
            this.userName = userName;
            this.password = password;
            this.email = email;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public User() {}
    }
  1. 建立 UserController 類並新增 @Controller@RequestMapping 註釋
    @Controller
    @RequestMapping(value = "api")
    public class UserController {
    }
  1. 定義靜態列表使用者變數以模擬資料庫並將 2 個使用者新增到列表中
    private static List<User> users = new ArrayList<User>();

    public UserController() {
        User u1 = new User(1L, "shijazi", "password", "shijazi88@gmail.com", "Safwan", "Hijazi");
        User u2 = new User(2L, "test", "password", "test@gmail.com", "test", "test");
        users.add(u1);
        users.add(u2);
    }
  1. 建立新方法以返回靜態列表中的所有使用者(getAllUsers)
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public @ResponseBody List<User> getAllUsers() {
         return users;
    }
  1. 執行應用程式[by mvn clean install spring-boot:run]並將此 URL 稱為 http://localhost:8080/api/users

  2. 我們可以用 @RestController 註釋類,在這種情況下我們可以從這個類中的所有方法中刪除 ResponseBody,(@RestController = @Controller + ResponseBody),如果我們使用 ResponseEntity,我們可以控制返回 http 程式碼的另一個點,我們將實現相同的先前函式但是使用 @RestControllerResponseEntity

@RestController
@RequestMapping(value = "api2")
public class UserController2 {

    private static List<User> users = new ArrayList<User>();

    public UserController2() {
        User u1 = new User(1L, "shijazi", "password", "shijazi88@gmail.com", "Safwan", "Hijazi");
        User u2 = new User(2L, "test", "password", "test@gmail.com", "test", "test");
        users.add(u1);
        users.add(u2);
    }
    
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public ResponseEntity<?> getAllUsers() {
       try {
           return new ResponseEntity<>(users, HttpStatus.OK);
       } catch (Exception e) {
           return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
       }
    }
}

現在嘗試執行該應用程式並呼叫此 URL http:// localhost:8080 / api2 / users