创建 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