【springboot】使用openfeign调用第三方接口示例

目录

          • [1. 示例](#1. 示例)
          • [2. pom依赖](#2. pom依赖)
          • [3. Application](#3. Application)
          • [4. controller](#4. controller)
          • [5. service](#5. service)
          • [6. client](#6. client)
1. 示例
复制代码
curl --location --request GET 'http://127.0.0.1:8080/example/test?id=1' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Accept: */*' \
--header 'Host: 127.0.0.1:8080' \
--header 'Connection: keep-alive'
2. pom依赖
复制代码
<?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.example</groupId>
    <artifactId>Learn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.34</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.52</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3. Application
复制代码
package com.learn;

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

/**
 * @date 2024/12/11
 * @description
 */
@SpringBootApplication
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
4. controller
复制代码
package com.learn.controller;

import com.learn.service.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    private ExampleService exampleService;

    @GetMapping("/data")
    public String data(@RequestParam String id) {
        return id;
    }

    @GetMapping("/test")
    public String test(@RequestParam String id) {
        return exampleService.test(id);
    }
}
5. service
复制代码
package com.learn.service;

import com.learn.client.ThirdClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExampleService {

    @Autowired
    private ThirdClient thirdClient;

    public String test(String id) {
        return thirdClient.getData(id);
    }
}
6. client
复制代码
package com.learn.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @date 2024/12/16
 * @description 第三方接口,这里演示为本机接口
 */
@FeignClient(name = "thirdClient", url = "http://localhost:8080")
public interface ThirdClient {

    @GetMapping("/example/data")
    String getData(@RequestParam("id") String id);
}
相关推荐
TinpeaV5 天前
(JAVA)自建应用调用企业微信API接口,实现消息推送
java·redis·企业微信·springboot·springflux
tanxiaomi8 天前
学习分库分表的前置知识:高可用系统架构理论与实践
java·mysql·spring cloud·系统架构·springboot
麦兜*14 天前
国产大模型平替方案:Spring Boot通义千问API集成指南
java·spring boot·后端·python·spring cloud·系统架构·springboot
core51214 天前
fastdfs快速部署、集成、调优
docker·部署·springboot·fastdfs·调用
尚学教辅学习资料15 天前
SpringBoot3.x入门到精通系列: 2.3 Web开发基础
前端·springboot·web开发
97zz15 天前
项目配置文件正确但是启动失败,报配置文件内容错误或中间件地址与实际不符
java·中间件·springboot
闫小甲15 天前
jobrunr xxljob 怎么选?
springboot·xxljob·jobrunr
core51215 天前
elk快速部署、集成、调优
elk·springboot·kibana·索引·查询
还是鼠鼠16 天前
tlias智能学习辅助系统--SpringAOP-进阶-通知顺序
java·后端·mysql·spring·mybatis·springboot
蜗牛031416 天前
2、RabbitMQ的5种模式基本使用(Maven项目)
java·springboot·java-rabbitmq