使用Spring创建REST应用
一、简单创建一个REST应用
构建一个简单的REST应用
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
先看代码:
UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/user/{id}",
produces = {
"application/json" })
public ResponseEntity<User> getUserById(@PathVariable("id") Integer id) {
return ResponseEntity.ok(userService.getUserById(id));
}
@GetMapping(value = "/users",
produces = {
"application/json" })
public ResponseEntity<List<User>> getUserList() {
return ResponseEntity.ok(userService.