Spring Boot:构建一个RESTful Web应用程序
介绍:
REST代表表示状态传输 ,是API设计的体系结构指南。 我们假设您已经具有构建RESTful API的背景。
在本教程中,我们将设计一个简单的Spring Boot RESTful Web应用程序,公开一些REST端点。
项目设置:
让我们首先通过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 {@Autowiredprivate 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(@RequestBodyStudent student) {Student newStudent = studentService.store(student);return new ResponseEntity<Student>(newStudent, HttpStatus.CREATED);}...
}
我们可以在控制器中定义所有的GET,POST,DELETE或PUT映射。
服务:
在这里, StudentService是与数据库交互并为我们执行所有操作的类:
@Service
public class StudentService {@Autowiredprivate 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
Spring Boot:构建一个RESTful Web应用程序相关推荐
- 使用React和Spring Boot构建一个简单的CRUD应用
"我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. Reac ...
- spring boot构建基础版web项目(一)springboot、thymeleaf控制层基础构
原文作者:弥诺R 原文地址:http://www.minuor.com/147852147/article 转载声明:转载请注明原文地址,注意版权维护,谢谢! 写前说明 根据个人在各篇博文中看到的信息 ...
- 使用React Native和Spring Boot构建一个移动应用
"我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. Reac ...
- SpringBoot 精通系列-构建一个RESTful Web 服务
导语 现在越来越多的企业推荐使用的是RESTful风格来构建企业应用接口,那么什么是RESTful呢? 文章目录 什么是RESTful SpringBoot对于RESTful有哪些支持 快速实例 ...
- vr设备应用程序_在15分钟内构建一个VR Web应用程序
vr设备应用程序 在15分钟内,您可以开发一个虚拟现实应用程序,并在Web浏览器,VR头盔或Google Daydream上运行它. 关键是A-Frame ,这是Mozilla VR Team构建的开 ...
- Spring Getting Started (1):构建一个RESTful的web服务
本文内容翻译自:http://spring.io/guides/gs/rest-service/ 创建一个资源表示类 为了给greeting的表示建模,我们创建一个资源表示类,它是一个POJO,带有i ...
- 使用Angular,Ionic 4和Spring Boot构建移动应用
朋友不允许朋友写用户身份验证. 厌倦了管理自己的用户? 立即尝试Okta的API和Java SDK. 在几分钟之内即可对任何应用程序中的用户进行身份验证,管理和保护. 我是Ionic的忠实粉丝. 几年 ...
- Spring微服务实战第2章 使用Spring Boot构建微服务
第2章 使用Spring Boot构建微服务 基于微服务的架构具有以下特点. 有约束的--微服务具有范围有限的单一职责集.微服务遵循UNIX的理念,即应用程序是服务的集合,每个服务只做一件事,并只做好 ...
- 使用Spring Boot构建微服务(文末福利)
本文主要内容 学习微服务的关键特征 了解微服务是如何适应云架构的 将业务领域分解成一组微服务 使用Spring Boot实现简单的微服务 掌握基于微服务架构构建应用程序的视角 学习什么时候不应该使用微 ...
最新文章
- Spring Boot Profile使用详解及配置源码解析
- 3个Python面试回答的技巧,助你面试大大加分
- 3.1集合相关知识点
- 11行代码AC——比紫书优化,例题2-3 近似计算——解题报告
- 客户端实时获取Oracle数据库服务器端的系统时间
- java实现迷你计算机,用JAVA写一个迷你编辑器.doc
- webpack系列-plugin
- Linux CentOS 7 安装 JAVA(jdk-8u181-linux-x64)
- 使用计算机教学的意义,信息技术在教学中的作用
- 法院才是最童叟无欺的一元店
- 我朋友坚持只肯以银行卡转账的方式还我钱是为什么
- Ext 3.1版本放出,可以免费下载了
- 连八股文都不懂还指望在后端混下去么
- svn commit 提示Aborting commit 失败问题解决办法
- 图像的空间分辨率和幅度分辨率
- Mahalanobis距离 Vs. 欧氏距离
- ECC算法的详细说明
- Linux安装tomcat,配置环境变量
- 《怪诞行为学》40条基本观点
- 腾达f3虚拟服务器怎么设置,腾达(Tenda)F3无线路由器设置【图文】教程大全
热门文章
- 深入源码分析Java线程池的实现原理
- MongoDB查询实现 笛卡尔积,Union All 和Union 功能
- 探究Java File类中list()、listFiles()的使用及区别,认识和使用匿名内部类
- Oracle入门(五A)之conn命令
- 递归算法介绍及Java应用实战
- $router VS $route
- arm linux gcc 编译,Linux arm-linux-gcc交叉编译环境配置
- win10系统excel2019单元格显示完整的年月日时分秒设置方法
- JDBC连接数据库教程,postgreSQL
- 计算机主机组成实验,计算机组成原理实验-运算器组成实验报告