【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);
}
相关推荐
S-X-S1 天前
「2024 博客之星」自研Java框架 Sunrays-Framework 使用教程
java·rabbitmq·springboot·web·log4j2·minio·脚手架
Watermelo6172 天前
使用JSONObject.getString()时报错:Cannot resolve method ‘getString‘ in ‘JSONObject‘,详解JSONObject三种库的用法
java·开发语言·spring boot·后端·java-ee·json·springboot
Hello Dam2 天前
Jmeter 动态参数压力测试时间段预定接口
jmeter·spring cloud·springboot·压力测试
小Mie不吃饭2 天前
彻底讲清楚 单体架构、集群架构、分布式架构及扩展架构
java·分布式·spring cloud·架构·springboot
一个松3 天前
配置正确spring-boot工程启动的时候报错dynamic-datasource Please check the setting of primary
maven·springboot
web2u6 天前
【鱼皮大佬API开放平台项目】Spring Cloud Gateway HTTPS 配置问题解决方案总结
vue.js·nginx·http·spring cloud·https·vue·springboot
IT机器猫11 天前
SpringCloud项目搭建快速入门
intellij-idea·springboot·springcloud·springalibaba
V+zmm1013413 天前
基于微信小程序的水果销售系统的设计与实现springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·springboot
华年源码14 天前
基于SpringBoot的旅游网站的设计与实现(源码+数据库+文档)
java·毕业设计·源码·springboot·旅游
风月歌14 天前
java项目之旅游网站的设计与实现(源码+文档)
java·mysql·vue·源码·springboot