• Guide2:Consuming a RESTful Web Service


    This guide walks you through the process of creating an application that consumes a RESTful web service.

    You will build an application that uses Spring 's RestTemplate to retrieve data at the first guide
    http://localhost:8080/greeting?name=#

    RestTemplate makes interacting with most RESTful services a one-line incantation. Adn it can even bind that data to custom domain types.

    1 创建新项目

    在这里插入图片描述

    2 创建实体类

    we need to create a domain class to contain the data that we need. now we create a class called Value.

    src/main/java/com/example/consumingrest/Value.java

    package com.example.consumingrest;
    
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import lombok.Data;
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    @Data
    public class Value {
        private Long id;
        private String content;
    
        public Value() {
        }
    
        @Override
        public String toString() {
            return "Value{" +
                    "id=" + id +
                    ", content='" + content + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    This simple Java class has a handful of properties and matching getter methods. It is annotated with @JsonIgnoreProperties from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored.

    @JsonIgnoreProperties:类注解,作用为使Json序列化时将javabean中的一些属性忽略掉。
    @JsonIgnoreProperties(ignoreUnknown = true):表示将忽略类中不存在的字段。
    在这里的作用:忽略从service端取出的数据中Value不包含的属性。

    To directly bind your data to your custom types, you need to specify the variable name to be exactly the same as the key in the JSON document returned from the API.

    In case your variable name and key in JSON doc do not match, you can use @JsonProperty annotation to specify the exact key of the JSON document.

    @JsonProperty:用于属性上,将属性的名称序列化为另一个名称。用法如下:
    @JsonProperty(“username”)
    private String name;

    3 修改端口号

    ①修改方式一:application.properties:

    server.port=8081
    
    • 1

    ②修改方式二:application.yml:

    server:
      port: 8081
    
    • 1
    • 2

    修改端口号是为了不与guide1冲突,使两个guide能同时运行。

    4 启动类

    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;
    
    @SpringBootApplication
    public 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 -> {
                Value value = restTemplate.getForObject(
                        "http://localhost:8080/greeting", Value.class);
                log.info(value.toString());
            };
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    we need to add a few other things to the ConsumingRestApplication class to get it to show data from our RESTful source.
    RestTemplate, which uses the Jackson JSON processing library to process the incoming data.
    CommandLineRunner, runs the RestTemplate on startup.

    5 运行

    同时运行guide1与guide2:
    在这里插入图片描述

    输出结果:Value{id=5,content=‘Hello World!’}

    6 目录结构

    在这里插入图片描述

  • 相关阅读:
    FPGA优质开源项目 – UDP万兆光纤以太网通信
    YOLOv8教程系列:五、关闭数据增强
    Dockerfile指令详解
    Qt之彻底解决QSpinBox限定范围无效的问题
    word简历排版技巧
    【从零开始学习 SystemVerilog】2.10、SystemVerilog 数据类型—— Associative Array(关联数组)
    linux安装mysql8
    Dubbo 原理和机制详解 (非常全面)
    windows环境下qt-material-widget开源库的编译
    十年测试老鸟带你玩转-测试用例
  • 原文地址:https://blog.csdn.net/qq_52077925/article/details/127785732