springboot实战之使用restfulweb服务

挑战者V      2022-04-07     504

关键词:

一、导入maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-consuming-rest</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

二、构建实体

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 +
                '}';
    }
}

 

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 + '\'' +
                '}';
    }
}

 

@JsonIgnore注解用来忽略某些字段,可以用在Field或者Getter方法上,用在Setter方法时,和Filed效果一样。这个注解只能用在POJO存在的字段要忽略的情况,不能满足现在需要的情况。

@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段,可以满足当前的需要。这个注解还可以指定要忽略的字段。使用方法如下:

@JsonIgnoreProperties({ "internalId", "secretKey" })
指定的字段不会被序列化和反序列化。

 

 

三、编写启动类

package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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());
    }

}

 

 

运行后控制台显示结果为:

 

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;

@SpringBootApplication
public 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中带有消息转换器和请求工厂的所有自动配置。我们还将其提取RestTemplate为a @Bean以使其更容易测试(可以通过这种方式更容易地进行模拟)。

 

最后执行mvn clean install或者mvn install,生成jar包,通过cmd命令执行java -jar  gs-consume-rest-0.1.0.jar

出现如红色标记处,表示成功开发一个RestFul客户端。

springboot实战之使用ldap验证用户

关于ubuntu16.04服务器安装配置LDAP参考链接为:https://www.howtoing.com/how-to-install-and-configure-openldap-and-phpldapadmin-on-ubuntu-16-04/本文主要讲LDAP相关的概念,普及相关知识和相关例子实战。一、LDAP服务器概念和原理1.目录服务目录是一个为... 查看详情

springboot实战之thymeleaf

...共同的职能是MVC模式中的视图展示层,即View。当然了,SpringBoot中也可以用jsp,不过不推荐这种用法,比较推崇的就是使用Thymeleaf。关于Thymeleaf学习,建议参考官方文档:https://www.thymeleaf.org/documentation.html官方文档例子,应有尽有。... 查看详情

springboot实战之接口日志篇

在本篇文章中不会详细介绍日志如何配置、如果切换另外一种日志工具之类的内容,只用于记录作者本人在工作过程中对日志的几种处理方式。1.Debug日志管理在开发的过程中,总会遇到各种莫名其妙的问题,而这些问题的定位... 查看详情

springboot2.x实战之定时任务调度

...务的,例如:定时同步一批数据、定时清理一些数据,在SpringBoot中提供了@Scheduled注解就提供了定时调度的功能,对于简单的、单机的调度方案是足够了的。这篇文章准备用实际案例看下@Scheduled的用法。开发实战新建SpringBoot工... 查看详情

springboot2.x实战之定时任务调度

...务的,例如:定时同步一批数据、定时清理一些数据,在SpringBoot中提供了@Scheduled注解就提供了定时调度的功能,对于简单的、单机的调度方案是足够了的。这篇文章准备用实际案例看下@Scheduled的用法。开发实战新建SpringBoot工... 查看详情

springboot实战系列之@value注解的使用心得

参考技术A在工作中使用springboot经常有属性注入的场景,下面说一下有默认值和无默认值两种写法的不同这中是有默认值的写法,默认是分号后的值,这里为true,但是如果在配置文件中(application.properties或application.yml)中设置了app... 查看详情

[译]springboot构建一个restfulweb服务

翻译地址:https://spring.io/guides/gs/rest-service/构建一个RESTfulWeb服务本指南将指导您完成使用spring创建一个“helloworld”RESTfulWeb服务的过程。 你将会构建什么您将构建一个将接受HTTPGET请求的服务:您将构建一个将接受HTTPGET... 查看详情

springboot实战之使用jdbc和spring访问数据库

这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数... 查看详情

springboot实战之使用jdbc和spring访问数据库

这里演示的是h2databse示例,所以简单的介绍普及下h2database相关知识H2数据库是一个开源的关系型数据库。H2是一个嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一个十分方便的web控制台用于操作和管理数... 查看详情

springboot实战(十三)之缓存

什么是缓存?引用下百度百科的解释:缓存就是数据交换的缓冲区(又称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,找到了则直接执行,找不到的话则从内存中查找。由于缓存的运行速度比内存快得多,故... 查看详情

springboot整合rabbitmq之发送接收消息实战

实战前言前几篇文章中,我们介绍了SpringBoot整合RabbitMQ的配置以及实战了Spring的事件驱动模型,这两篇文章对于我们后续实战RabbitMQ其他知识要点将起到奠基的作用的。特别是Spring的事件驱动模型,当我们全篇实战完毕RabbitMQ并... 查看详情

springboot实战之url传参

参考技术A其中required=false是说明这个字段可以传入也可以不传入,如果不这样写,就会匹配version字段,匹配不到,就会报错,这时,如果访问"/index"路径也会报错。【注意】要点击@PathVariable注解进去看一下,是否支持required参数... 查看详情

springboot+vue+实战项目之第1集(代码片段)

SpringBoot+vue+实战项目--锋迷商城1.项目功能2.技术选型3.项目架构的演进4.SpringBoot5.Usercontroller(实操注册用户)6.Java配置方式6.1xml配置6.2注解配置6.3Java配置⽅式(第三方类对象)7.SpringBoot自动配置(底层... 查看详情

springboot实战实现分布式锁一之重现多线程高并发场景

实战前言:上篇博文我总体介绍了我这套视频课程:“SpringBoot实战实现分布式锁”总体涉及的内容,从本篇文章开始,我将开始介绍其中涉及到的相关知识要点,感兴趣的小伙伴可以关注关注学习学习!!工欲善其事,必先利... 查看详情

springwebflux构建响应式restfulweb服务(代码片段)

...您完成创建“Hello,Spring!”使用SpringWebFlux的rest式web服务(SpringBoot2.0的新版本),然后使用WebClient使用该服务(SpringBoot2.0的新版本)。1、你将建立什么您将使用SpringWebflux和该服务的WebClient使用者构建一个RESTfulweb服务。您将能够在... 查看详情

springboot2.0实战(23)整合redis之集成缓存springdatacache

SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情

springboot实战之rabbitmq

什么是RabbitMQ?RabbitMQ是一个消息代理。它的核心原理非常简单:接收和发送消息。你可以把它想像成一个邮局:你把信件放入邮箱,邮递员就会把信件投递到你的收件人处。在这个比喻中,RabbitMQ就扮演着邮箱、邮局以及邮递员... 查看详情

springboot实战之validator

表单验证,是最为常见的,今天演示的是利用hibernate-validtor进行校验,有的时候,虽然前端方面通过jQuery或者require.js校验框架进行校验,可以减轻服务器的压力和改善用户体验,但是比如第三方请求接口方面也需要校验,在Contr... 查看详情