Spring Boot:消费一个RESTful的网络服务(译)
来源:互联网 发布:知乎周刊 epub 编辑:程序博客网 时间:2024/06/06 06:39
Spring Boot:消费一个RESTful的网络服务(译)
从github上下载资源 https://github.com/spring-guides/gs-consuming-rest.git
原文地址:https://spring.io/guides/gs/consuming-rest/
目录
- Spring Boot消费一个RESTful的网络服务译
- 目录
- 获得网站上rest资源
- 让项目运行起来
- 运用Spring Boot来管理应用的生命周期
- 构建一个可以运行的jar包
- 资源和原文链接
获得网站上rest资源
当完成这个项目时,你能够创建一个简单的应用来消费一个RESTful的服务。
一个restful的服务已经在以下链接中提供了: http://gturnquist-quoters.cfapps.io/api/random,链接随机的摘取了一部分关于Spring Boot引用,并且返回一个JSON格式的文件。
如果你通过浏览器请求这个URL地址,你会获得以下的JSON信息。
{ type: "success", value: { id: 10, quote: "Really loving Spring Boot, makes stand alone Spring apps easy." }}
虽然这样做很简单,但是需要通过浏览器来获得资源不是很方便。
更方便的一种方式是通过代码的来消费RESTful的网络服务,为了方便你消费RESTful的网络服务spring提供了一个方便的模版类叫 RestTemplate, RestTemplate类让代码和大多数的RESTful的服务的交互变得简单,只需要一行的代码就能完成。它甚至可以将RESTful服务内的数据和一个简单的java类进行绑定。
1,首先,创建一个包含你需要数据的domain类。
src/main/java/hello/Quote.java
package hello;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 + '}'; }}
你可以看到上面的类是一个带有一部分属性和对应getter,setter方法的简单java类。它的上面带有@JsonIgnoreProperties(ignoreUnknown = true) 的注解,来自Jackson JSON处理库,注解暗示了任何没有与这个类型绑定的属性都将被忽略。
为了直接绑定数据和对应的java类,java类中变量的名称需要和API返回的JSON文档中健值的名称相同。如果两者不相同,那么需要加上@JsonProperty的注解,来指定JSON文档中健值的名字。
另外,需要将上述Quote类嵌入到另一个类的的内部引用中:
src/main/java/hello/Value.java
package hello;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 + '\'' + '}'; }}
上面的类使用了相同注解。
让项目运行起来
虽然我们可以把这个服务打成一个传统的 war包去部署到一个外部的应用服务器上, 但是下面有一个更加简单的操作,来创建一个独立运行的应用。你可以将所有文件打包成一个简单的,可运行的jar包,并用一个好用的main()方法来驱动。用这种方式,你可以使用Spring的支持,内嵌一个tomcat servlet的容器作为HTTP的runtime, 来代替部署一个外部的tomcat实例。
现在你可以编写 Application 的类, 并在这个类中,通过 RestTemplate从外部服务中获取数据。
src/main/java/hello/Application.java
package hello;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.client.RestTemplate;public class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String args[]) { RestTemplate restTemplate = new RestTemplate(); Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class); log.info(quote.toString()); }}
因为Jackson中JSON处理库在类路径中(classpath),RestTemplate会使用它(通过信息转化器)转化即将到达的JSON数据成一个Quote的实例,从这里,Quote对象的内容会被打印到控制台上。
现在你已经使用RestTemplate来发送一个get请求了,另外RestTemplate类还支持POST, PUT,和DELETE请求。
运用Spring Boot来管理应用的生命周期
到目前为止,你都没有在你的应用中使用Spring Boot, 但是如果使用Spring Boot会有很多的好处,并且使用Spring Boot不难。其中一个优势是我们可以让Spring Boot帮我们在RestTemplate 类中管理消息转化器,这样的话,能很容易添加自定义的内容。为了加入Spring Boot,我们需要在main的类中添加@SpringBootApplication的注解,转化main方法为启动Spring Boot的方法,就像Spring Boot 的应用。最后我们移动RestTemplate到CommandLineRunner的回调方法中,这样的话,RestTemplate能在Spring Boot启动时执行:
src/main/java/hello/Application.java
package hello;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 Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String args[]) { SpringApplication.run(Application.class); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Bean public CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { Quote quote = restTemplate.getForObject( "http://gturnquist-quoters.cfapps.io/api/random", Quote.class); log.info(quote.toString()); }; }}
RestTemplateBuilder这个类被Spring容器注入,并且如果你使用这个类来创建RestTemplate那么你会受益于Spring Boot提供的对信息转化器(message converters)和请求工厂(request factories)的自动配置机制。我们还可以提取RestTemplate为一个@Bean,然后我们能够更方便的进行测试。
构建一个可以运行的jar包
你可以在命令行使用Gradle或者Maven来运行你的应用,另外你还可以构建一个简单的可执行的包含所有必要依赖包,类和资源的jar包,并且运行它。构建一个独立的jar包能够更加方便地进行转移,版本控制,部署,以及运用在不同的环境。
如果你使用的是Gradle那么你可以通过 ./gradlew bootRun .命令来运行应用,或者通过./gradlew build 命令来构建独立的jar包,并通过
java -jar build/libs/gs-consuming-rest-0.1.0.jar 命令来运行jar包。
如果你使用的是Maven那么你可以通过 ./mvnw spring-boot:run .命令来运行应用,或者通过./mvnw clean package 命令来构建独立的jar包,并通过
java -jar target/gs-consuming-rest-0.1.0.jar 命令来运行jar包。
当运行成功时,你将看到如下格式的输出(输出中有一段随机的引用)
2015-09-23 14:22:26.415 INFO 23613 --- [main] hello.Application : Quote{type='success', value=Value{id=12, quote='@springboot with @springframework is pure productivity! Who said in #java one has to write double the code than in other langs? #newFavLib'}}If you see the error Could not extract response: no suitable HttpMessageConverter found for response type [class hello.Quote] it’s possible you are in an environment that cannot connect to the backend service (which sends JSON if you can reach it). Maybe you are behind a corporate proxy? Try setting the standard system properties http.proxyHost and http.proxyPort to values appropriate for your environment.
资源和原文链接
从github上下载资源 https://github.com/spring-guides/gs-consuming-rest.git
原文地址:https://spring.io/guides/gs/consuming-rest/
- Spring Boot:消费一个RESTful的网络服务(译)
- 使用RestTemplate消费spring boot的Restful服务
- 用Kotlin写一个基于Spring Boot的RESTful服务
- 用Kotlin写一个基于Spring Boot的RESTful服务
- 一个基于Spring Boot的API、RESTful API项目
- Spring Boot 构建一个RESTful Web服务
- Spring Boot创建一个RESTful Web Service
- Spring Boot 构建一个较为复杂的RESTful API以及单元测试
- spring boot demo( 获取一个RESTful web service)
- 使用spring boot快速构建一个RESTful Web Service
- Spring boot 使用restful风格的spring security login
- 一、Spring-boot设置restful
- spring boot 实现Restful API
- Spring boot restful api demo
- spring boot restful API风格
- Eclipse使用Spring Boot的Restful微服务
- Spring Boot-RESTful简单项目的快速搭建
- Spring boot整合Springfox在线生成restful的api doc
- CentOS7下rsync实现服务器之间实时同步
- 循环链表的经典问题
- 图-邻接矩阵
- USB HID设备报告描述符详解
- 29_Break rules
- Spring Boot:消费一个RESTful的网络服务(译)
- 1.Python进阶笔记——jython
- 单元测试方法的使用
- There is insufficient memory for the Java Runtime Environment to continue
- mysql 5.3.9 for mac 忘记密码
- gradle项目发布到自定义的tomcate中
- ubuntu16.04_tensorflow_gpu加速_源码安装笔记
- 2.笔记Erlang语言——基本操作
- C语言指针详细解答