惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

博客园 - strong

Mac电脑上传ZIP图片压缩包时垃圾文件清理问题解决 帮一个朋友解决https证书过期的问题 电脑开不了机了,最后一检查发现是硬盘烧坏了 CentOS 上安装 Docker(完整步骤) SpringBoot项目接入分布式任务调度平台xxl-job(2.0.2)说明 记录一次错误的使用当前时间new Date()引发的错误 错误提示“com.alibaba.fastjson.JSONException: exepct '[', but string, pos 4, json”解决 PaaS和SaaS的区别是什么? 数据库链接失败错误ERROR com.alibaba.druid.pool.DruidDataSource - {dataSource-1} init error解决 WampServer3.0服务器端开启ssl认证后重启Apache失败,解决办法 用户中心 - 博客园 在SublimeText3中想使用快捷键调出插件ColorPicker不起作用办法解决 【转】wamp如何添加多个站点 在sublime text 3中编译javascript 如何在Axure RP 8.0 中打开页面指定的动态面板 打开Access时电脑出现蓝屏,错误编号0x00000116的问题解决 windows下解决mysql5中文乱码的问题 在控制台中输入msqyl一直报ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost'错误 eclipse 提示错误The method of type must override a superclass method 的解决办法
老项目 Java EE + Spring MVC(非 Spring Boot)使用openfeign
strong · 2025-11-28 · via 博客园 - strong

老项目 Java EE + Spring MVC(非 Spring Boot)使用feign


一、前言

 大家知道,在SpringBoot项目中使用Feign比如简单,只需要开启@FeignClient注解,就可以调用了,但在一些SpringMVC项目中使用就比较麻烦,本文将介绍在老项目中如何相对简单的使用Feign。


二、Feign 是什么?

Feign 是一个声明式 HTTP 调用框架,可以像调用本地方法一样调用远程服务。

  • 无需写 HttpClient / RestTemplate
  • 支持接口调用
  • 支持注解绑定参数

三、环境说明(可选)

  • JDK 1.8+
  • 非 Spring Boot 项目 / 老项目
  • 老项目无法轻易引入 Spring Cloud 体系,所以选用原生 Feign(feign-core)而不是 OpenFeign

四、添加 Maven 依赖

<!-- Feign 核心 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>12.5</version>
</dependency>

<!-- Feign Gson 支持 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-gson</artifactId>
    <version>12.5</version>
</dependency>

<!-- 日志输出(Slf4j) -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-slf4j</artifactId>
    <version>12.5</version>
</dependenc

五、定义 Feign 接口(API Client 层)

例如要访问一个“用户服务”的 HTTP 接口:

package com.example.client;

import feign.Headers;
import feign.RequestLine;

public interface UserApiClient {

    @RequestLine("GET /api/user/{id}")
    UserResponse getUserById(@Param("id")Long id);

    @RequestLine("POST /api/user/create")
    @Headers("Content-Type: application/json")
    CreateUserResponse createUser(CreateUserRequest request);
}

六、构建 Feign 客户端(在 Service 中)


package com.example.service;

import com.example.client.UserApiClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import feign.Feign;
import feign.Logger;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class UserApiService {

    @Value("${user.api.base-url}")
    private String baseUrl;

    private UserApiClient buildClient() {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        return Feign.builder()
                .encoder(new GsonEncoder(gson))
                .decoder(new GsonDecoder(gson))
                .logger(new Slf4jLogger(UserApiClient.class))
                .logLevel(Logger.Level.FULL) // 输出详细日志
                .target(UserApiClient.class, baseUrl);
    }

    public UserResponse getUser(Long id) {
        return buildClient().getUserById(id);
    }

    public CreateUserResponse createUser(CreateUserRequest req) {
        return buildClient().createUser(req);
    }
}

  注意在 application.properties 文件中添加  user.api.base-url=http://example.com


七、调用示例

UserResponse user = userApiService.getUser(1001L);

CreateUserRequest req = new CreateUserRequest();
req.setName("Tom");
req.setEmail("tom@example.com");
CreateUserResponse resp = userApiService.createUser(req);

八、总结

  • 非 Spring Boot 项目依然能轻松接入 Feign
  • Feign 使用简单,避免 RestTemplate + HttpClient 的繁琐代码
  • 适合老项目、war 包项目
  • 无需改造架构即可使用

如果本文对你有帮助,欢迎点赞、收藏、关注!