Guide2:Consuming a RESTful Web Service

"本文指导如何使用Spring的RestTemplate创建一个应用程序,通过实例展示了如何从RESTful服务获取数据,并将数据绑定到自定义类型。步骤包括创建实体类、修改端口号、配置启动类和运行应用,最终输出了数据实例:Value{id=5,content='HelloWorld!'}
摘要由CSDN通过智能技术生成

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

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

②修改方式二:application.yml:

server:
  port: 8081

修改端口号是为了不与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());
        };
    }
}

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 目录结构

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值