使用RESTful Web服务的过程

本指南将引导您完成创建使用#spring# #spring认证# restful web 服务的应用程序的过程。
你将建造什么 您将构建一个应用程序,该应用程序使用 springresttemplate在
https://quoters.apps.pcfone.io/api/random检索随机 spring boot 报价。
你需要什么 约15分钟 最喜欢的文本编辑器或 ide jdk 1.8或更高版本 gradle 4+或maven 3.2+ 您还可以将代码直接导入 ide: 弹簧工具套件 (sts) intellij idea 如何完成本指南 像大多数 spring入门指南一样,您可以从头开始并完成每个步骤,也可以绕过您已经熟悉的基本设置步骤。无论哪种方式,您最终都会得到工作代码。
要从头开始,请继续从 spring initializr 开始。
要跳过基础知识,请执行以下操作:
下载并解压缩本指南的源存储库,或使用git克隆它:git clone https://github.com/spring-guides/gs-consuming-rest.git 光盘进入gs-consuming-rest/initial 跳转到获取 rest 资源。 完成后,您可以对照中的代码检查结果
gs-consuming-rest/complete。
从 spring initializr 开始 您可以使用这个预先初始化的项目并单击 generate 下载 zip 文件。此项目配置为适合本教程中的示例。
手动初始化项目:
导航到https://start.spring.io。该服务提取应用程序所需的所有依赖项,并为您完成大部分设置。 选择 gradle 或 maven 以及您要使用的语言。本指南假定您选择了 java。 单击dependencies并选择spring web。 单击生成。 下载生成的 zip 文件,该文件是根据您的选择配置的 web 应用程序的存档。 如果您的 ide 具有 spring initializr 集成,您可以从您的 ide 完成此过程。
你也可以从 github 上 fork 项目并在你的 ide 或其他编辑器中打开它。
获取 rest 资源 完成项目设置后,您可以创建一个使用 restful 服务的简单应用程序。
一个 restful 服务已经在
https://quoters.apps.pcfone.io/api/random建立起来。它随机获取有关 spring boot 的引用并将它们作为 json 文档返回。
如果您通过 web 浏览器或 curl 请求该 url,您会收到如下所示的 json 文档:
{ type: success, value: { id: 10, quote: really loving spring boot, makes stand alone spring apps easy. }}复制 这很容易,但在通过浏览器或 curl 获取时并不是非常有用。
以编程方式使用 rest web 服务的更有用的方法。为了帮助您完成这项任务,spring 提供了一个方便的模板类,称为resttemplate. resttemplate使与大多数 restful 服务的交互成为单行咒语。它甚至可以将该数据绑定到自定义域类型。
首先,您需要创建一个域类来包含您需要的数据。以下清单显示了quote可以用作域类的类:
src/main/java/com/example/consumingrest/quote.java
package com.example.consumingrest;import com.fasterxml.jackson.annotation.jsonignoreproperties;@jsonignoreproperties(ignoreunknown = true)public class quote { private string type; private value value; public quote() { } public string gettype() { return type; } public void settype(string type) { this.type = type; } public value getvalue() { return value; } public void setvalue(value value) { this.value = value; } @override public string tostring() { return quote{ + type=' + type + '\'' + , value= + value + '}'; }}复制 这个简单的 java 类有一些属性和匹配的 getter 方法。它带有@jsonignoreproperties来自 jackson json 处理库的注释,表示任何未绑定在此类型中的属性都应被忽略。
要将您的数据直接绑定到您的自定义类型,您需要将变量名称指定为与从 api 返回的 json 文档中的键完全相同。如果您的 json 文档中的变量名称和键不匹配,您可以使用@jsonproperty注释来指定 json 文档的确切键。(此示例将每个变量名称与 json 键匹配,因此此处不需要该注释。)
您还需要一个额外的类来嵌入内部引用本身。该类value满足了这一需求,并显示在以下清单 (at
src/main/java/com/example/consumingrest/value.java) 中:
package com.example.consumingrest;import com.fasterxml.jackson.annotation.jsonignoreproperties;@jsonignoreproperties(ignoreunknown = true)public class value { private long id; private string quote; public value() { } public long getid() { return this.id; } public string getquote() { return this.quote; } public void setid(long id) { this.id = id; } public void setquote(string quote) { this.quote = quote; } @override public string tostring() { return value{ + id= + id + , quote=' + quote + '\'' + '}'; }}复制 这使用相同的注释,但映射到其他数据字段。
完成申请 initalizr 创建一个带有main()方法的类。以下清单显示了 initializr 创建的类(at
src/main/java/com/example/consumingrest/consumingrestapplication.java):
package com.example.consumingrest;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;@springbootapplicationpublic class consumingrestapplication { public static void main(string[] args) { springapplication.run(consumingrestapplication.class, args); }}复制 现在您需要向consumingrestapplication该类添加一些其他内容,以使其显示来自我们 restful 源的引用。您需要添加:
一个记录器,用于将输出发送到日志(在此示例中为控制台)。 a resttemplate,它使用 jackson json 处理库来处理传入的数据。 a在启动commandlinerunner时运行resttemplate(并因此获取我们的报价)。 以下清单显示了完成的consumingrestapplication类 (at
src/main/java/com/example/consumingrest/consumingrestapplication.java):
package com.example.consumingrest;import org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.boot.commandlinerunner;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.boot.web.client.resttemplatebuilder;import org.springframework.context.annotation.bean;import org.springframework.web.client.resttemplate;@springbootapplicationpublic class consumingrestapplication { private static final logger log = loggerfactory.getlogger(consumingrestapplication.class); public static void main(string[] args) { springapplication.run(consumingrestapplication.class, args); } @bean public resttemplate resttemplate(resttemplatebuilder builder) { return builder.build(); } @bean public commandlinerunner run(resttemplate resttemplate) throws exception { return args -> { quote quote = resttemplate.getforobject( https://quoters.apps.pcfone.io/api/random, quote.class); log.info(quote.tostring()); }; }}复制 运行应用程序 您可以使用 gradle 或 maven 从命令行运行应用程序。您还可以构建一个包含所有必要依赖项、类和资源的单个可执行 jar 文件并运行它。构建可执行 jar 可以在整个开发生命周期、跨不同环境等中轻松地作为应用程序交付、版本化和部署服务。
如果您使用 gradle,则可以使用./gradlew bootrun. 或者,您可以使用构建 jar 文件./gradlew build,然后运行 ​jar 文件,如下所示:
java -jar build/libs/gs-sumption-rest-0.1.0.jar 如果您使用 maven,则可以使用./mvnw spring-boot:run. 或者,您可以使用构建 jar 文件,./mvnw clean package然后运行该 jar 文件,如下所示:
java -jar 目标/gs-消费-rest-0.1.0.jar 此处描述的步骤创建了一个可运行的 jar。您还可以构建经典的 war 文件。
您应该看到类似于以下的输出,但带有随机引用:
2019-08-22 14:06:46.506 info 42940 --- [main] cecconsumingrestapplication : quote{type='success', value=value{id=1 如果您看到显示为 的错误,could not extract response: no suitable httpmessageconverter found for response type [class
com.example.consumingrest.quote]则可能是您处于无法连接到后端服务的环境中(如果您可以访问它,它将发送 json)。也许您是公司代理的幕后黑手。尝试将http.proxyhost和http.proxyport系统属性设置为适合您的环境的值。
概括 恭喜!您刚刚使用 spring boot 开发了一个简单的 rest 客户端。


华硕将于2月底发布电竞显示器ROG Swift PG248Q 原生支持超高刷新率
汽车动力控制系统研发企业菱电电控发布2022第一季度报告
高压差分探头N1030B的可替代国际品牌
华为创新基地“天工实验室”正式揭牌
加快产业优化升级 大力推进两化融合
使用RESTful Web服务的过程
关于红外遥控编码与解码的详细解析
语音识别芯片在产品应用上的难点列举
传微软Surface平板电脑将在苏宁独家发售
为什么说眼下距离真正的智能路由器市场还有很大一段距离?
光学影像仪如何选择?
了解机器数据如何收集、聚合和使用
[图文]胆管精确配对法
云主机特点
英飞凌与Kontrol携手提升自动驾驶汽车安全性
智能魔镜显示屏的应用将家居设备进行一键控制
全球晶圆资本支出紧追台积
电驱动NVH的特点和结构
列举2018年最值得关注的15大技术趋势
工业4.0时代,中国制造如何弯道超车?