创建 REST 服务
- 使用 STS(Spring Starter Project)或 Spring Initializr( https://start.spring.io ) 创建项目。
- 在 pom.xml 中添加 Web 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
或在 Search for dependencies
搜索框中键入 web ,添加 Web 依赖项并下载压缩项目。
- 创建域类(即用户)
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() {}
}
- 创建 UserController 类并添加
@Controller
,@RequestMapping
注释
@Controller
@RequestMapping(value = "api")
public class UserController {
}
- 定义静态列表用户变量以模拟数据库并将 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);
}
- 创建新方法以返回静态列表中的所有用户(getAllUsers)
@RequestMapping(value = "users", method = RequestMethod.GET)
public @ResponseBody List<User> getAllUsers() {
return users;
}
-
运行应用程序[by
mvn clean install spring-boot:run
]并将此 URL 称为http://localhost:8080/api/users
-
我们可以用
@RestController
注释类,在这种情况下我们可以从这个类中的所有方法中删除 ResponseBody,(@RestController = @Controller + ResponseBody)
,如果我们使用ResponseEntity
,我们可以控制返回 http 代码的另一个点,我们将实现相同的先前函数但是使用@RestController
和ResponseEntity
@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