京东自营 618 + 国补 iPhone 历史最低价

实例详解 Spring Boot 中的3类 HTTP 客户端用法

Spring Boot 3.3 引入了多项改进和新特性,其中包括对HTTP客户端的增强。Spring Boot 3.3 依赖于Spring Framework 6.0,后者对WebClientRestTemplate等HTTP客户端提供了更好的支持和改进。以下是一些使用Spring Boot 3.3中HTTP客户端的详细示例:

1. 使用 RestTemplate

尽管RestTemplate在Spring 6和Spring Boot 3中并不是推荐的新客户端(因为WebClient是非阻塞的,更适合现代应用),但它仍然可用。

示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RestTemplateExample implements CommandLineRunner {

    @Autowired
    private RestTemplate restTemplate;

    public static void main(String[] args) {
        SpringApplication.run(RestTemplateExample.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String url = "https://nowjava.com/nowjava/1";
        ResponseEntity<Post> response = restTemplate.getForEntity(url, Post.class);
        Post post = response.getBody();
        System.out.println(post);
    }

    // 定义Post类来映射JSON响应
    static class Post {
        private long id;
        private String userId;
        private String title;
        private String body;
        // getters and setters
    }
}

// 配置RestTemplate Bean(通常在配置类中完成)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

2. 使用 WebClient

WebClient是Spring WebFlux提供的非阻塞HTTP客户端,是Spring 6和Spring Boot 3中推荐的HTTP客户端。

示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@SpringBootApplication
public class WebClientExample implements CommandLineRunner {

    @Autowired
    private WebClient.Builder webClientBuilder;

    public static void main(String[] args) {
        SpringApplication.run(WebClientExample.class, args);
    }

    @Override
    public void run(String... args) {
        WebClient webClient = webClientBuilder.baseUrl("https://nowjava.com").build();
        Mono<Post> postMono = webClient.get()
                .uri("/nowjava/1")
                .retrieve()
                .bodyToMono(Post.class);

        postMono.subscribe(post -> System.out.println(post));
    }

    // 定义Post类来映射JSON响应
    static class Post {
        private long id;
        private String userId;
        private String title;
        private String body;
        // getters and setters
    }
}

// WebClient.Builder通常通过Spring上下文自动配置,无需手动创建

3. 使用 Feign 客户端(需要额外依赖)

Feign是一个声明式的Web服务客户端,它使得写HTTP客户端变得更简单。Spring Cloud OpenFeign提供了对Feign的集成。

示例

首先,添加Feign依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

然后,启用Feign客户端:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class FeignClientExample {
    public static void main(String[] args) {
        SpringApplication.run(FeignClientExample.class, args);
    }
}

定义Feign客户端接口:

展开阅读全文

本文系作者在时代Java发表,未经许可,不得转载。

如有侵权,请联系nowjava@qq.com删除。

编辑于

关注时代Java

关注时代Java