Spring Boot:构建一个RESTful Web应用程序

介绍:

REST代表表示状态传输 ,是API设计的体系结构指南。 我们假设您已经具有构建RESTful API的背景。

在本教程中,我们将设计一个简单的Spring Boot RESTful Web应用程序,公开一些REST端点。

项目设置:

Spring Boot:构建一个RESTful Web应用程序

让我们首先通过Spring Initializr下载项目模板

对于RESTful Web应用程序,我们只需要添加“ Spring Web”作为额外的入门依赖。 假设我们也在与数据库进行交互,则添加了其他两个。

现在,我们的POM文件将具有所有需要的Web应用程序和数据库依赖性:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

REST控制器:

现在让我们定义REST控制器:

@RestController
@RequestMapping("/student")
public class StudentController {
 
    @Autowired
    private StudentService studentService;
 
    @GetMapping("/all")
    public ResponseEntity<List<Student>> getAllStudents() {
        return new ResponseEntity<List<Student>>(studentService.getAllStudents()
            , HttpStatus.OK);
    }
 
    @GetMapping("/{id}") 
    public ResponseEntity<Student> getStudentById(@PathVariable("id") Integer id) {
        Optional<Student> student = studentService.getById(id);
        if(student.isPresent())
            return new ResponseEntity<Student>(student.get(), HttpStatus.OK);
        else 
            throw new ResponseStatusException(HttpStatus.NOT_FOUND
              , "No student found!"); 
    }
 
    @PostMapping("/")
    public ResponseEntity<Student> createStudent(@RequestBody
     Student student) {
        Student newStudent = studentService.store(student);
        return new ResponseEntity<Student>(newStudent, HttpStatus.CREATED);
    }
    
    ...
}

我们可以在控制器中定义所有的GET,POST,DELETEPUT映射。

服务:

在这里, StudentService是与数据库交互并为我们执行所有操作的类:

@Service
public class StudentService {
    @Autowired
    private StudentRepository repo;
    
    public Student store(Student student) {
        return repo.save(student);
    }
 
    public List<Student> getAllStudents() {
        return repo.findAll();
    }
 
    ...
 
}

我们还有另一本教程,说明如何使用Spring Boot配置H2数据库。

运行应用程序:

最后,我们可以运行我们的UniversityApplication类:

@SpringBootApplication
public class UniversityApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(UniversityApplication.class, args);
    }
}

通过它,我们的REST端点将在嵌入式服务器上公开。

测试REST端点:

让我们使用cURL来测试我们的REST端点:

$ curl http://localhost:8080/student/all

这将返回数据库中存在的所有学生记录:

[{1, "James"}, {2, "Selena"}, {3, "John"}]

同样,我们有:

$ curl http://localhost:8080/student/1
{1, "James"}

我们还可以使用POSTman工具来测试我们的端点。 它具有出色的用户界面。

结论:

在本教程中,我们从头开始构建了一个Spring Boot RESTful应用程序。 我们公开了一些API,然后使用cURL对其进行了测试。

翻译自: https://www.javacodegeeks.com/2019/09/spring-boot-building-restful-web-application.html