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 libraryto 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
annotationto 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!’}