当前位置: 首页 > news >正文

专门做推广的网站吗哪个网站可以改字体

专门做推广的网站吗,哪个网站可以改字体,拼多多关键词排名在哪里看,wordpress 咚门下载目录 一、环境准备 二、启用Feign客户端 三、定义Feign客户端接口 四、定义请求/响应DTO 五、调用Feign客户端 六、高级配置 1. 添加请求头(如认证) 2. 超时配置(application.yml) 3. 日志配置 七、错误处理 自定义错误…

目录

一、环境准备

二、启用Feign客户端

三、定义Feign客户端接口

四、定义请求/响应DTO

五、调用Feign客户端

六、高级配置

1. 添加请求头(如认证)

2. 超时配置(application.yml)

3. 日志配置

七、错误处理

自定义错误解码器

八、测试调用

常见问题解决


以下是Spring Boot项目通过Feign调用第三方接口的详细教程,包含完整步骤和代码示例:


一、环境准备

  1. 创建Spring Boot项目
    使用Spring Initializr生成项目,选择依赖:

    • Spring Web
    • OpenFeign
  2. pom.xml依赖

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>3.1.3</version> <!-- 与Spring Boot版本匹配 --></dependency>
    </dependencies>
    


二、启用Feign客户端

在启动类添加注解:

@SpringBootApplication
@EnableFeignClients // 关键注解
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}


三、定义Feign客户端接口

创建接口声明第三方API调用:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;@FeignClient(name = "thirdPartyApi", url = "https://api.example.com" // 第三方接口基地址
)
public interface ThirdPartyClient {// 示例:GET请求@GetMapping("/users/{id}")UserResponse getUser(@PathVariable("id") Long userId);// 示例:POST请求@PostMapping("/orders")OrderResponse createOrder(@RequestBody OrderRequest request);
}


四、定义请求/响应DTO

// 请求示例
public class OrderRequest {private String productId;private Integer quantity;// getters/setters
}// 响应示例
public class UserResponse {private Long id;private String name;private String email;// getters/setters
}


五、调用Feign客户端

在Service中注入并使用:

@Service
public class ApiService {@Autowiredprivate ThirdPartyClient thirdPartyClient; // 注入Feign客户端public UserResponse fetchUser(Long userId) {return thirdPartyClient.getUser(userId); // 调用第三方API}public void createNewOrder(OrderRequest request) {OrderResponse response = thirdPartyClient.createOrder(request);System.out.println("Order created: " + response.getOrderId());}
}


六、高级配置

1. 添加请求头(如认证)
@FeignClient(name = "authApi", url = "https://api.example.com")
public interface AuthClient {@GetMapping("/profile")ProfileResponse getProfile(@RequestHeader("Authorization") String token // 动态传递Header);
}

2. 超时配置(application.yml)
feign:client:config:default:connectTimeout: 5000  # 连接超时(ms)readTimeout: 10000     # 读取超时(ms)

3. 日志配置
@Configuration
public class FeignConfig {@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL; // 输出完整请求日志}
}

application.yml启用日志:

logging:level:com.example.demo.client.ThirdPartyClient: DEBUG


七、错误处理

自定义错误解码器
public class CustomErrorDecoder implements ErrorDecoder {@Overridepublic Exception decode(String methodKey, Response response) {if (response.status() == 404) {return new ResourceNotFoundException("API resource not found");}return new FeignException.BadRequest("API request failed");}
}

在配置类注册:

@Bean
public ErrorDecoder errorDecoder() {return new CustomErrorDecoder();
}


八、测试调用

@RestController
public class DemoController {@Autowiredprivate ApiService apiService;@GetMapping("/user/{id}")public UserResponse getUser(@PathVariable Long id) {return apiService.fetchUser(id);}
}


常见问题解决

  1. 404错误

    • 检查第三方URL是否正确
    • 确认接口路径是否包含上下文路径(如/api/v1
  2. 超时问题
    调整配置:

    ribbon:ConnectTimeout: 3000ReadTimeout: 60000
    

  3. JSON解析错误
    确保DTO字段名与JSON属性名匹配,或使用@JsonProperty注解

  4. 启用GZIP压缩(提升性能)

    feign:compression:request:enabled: trueresponse:enabled: true
    

提示:实际调用前建议使用Postman测试第三方接口可用性。

http://www.dtcms.com/a/474335.html

相关文章:

  • 小迪安全v2023学习笔记(一百零三讲)—— 漏扫项目篇PoC开发Rule语法反链判断不回显检测Yaml生成
  • MySQL的练习题二----创建表的练习题
  • 设备技术支持东莞网站建设大连模板网站制作多少钱
  • Dockerfile 指令详解与实战指南
  • 10.1 快速排序(排序(下))
  • 前端梳理体系从常问问题去完善-框架篇(Vue2Vue3)
  • 【数据分享】青藏高原10米分辨率DEM数据集
  • 网站正在建设中空白模板网站建设调查通知
  • 【星闪】Hi2821 | 低功耗开发 + 低功耗管理及按键唤醒例程
  • 怎么做网站的网盘品牌网站建设公司有哪些
  • 【Linux】Socket编程基础
  • 长春建网站wordpress 图片打开慢
  • 91工业设计网模板网站有利于做seo吗
  • 做网站蓝色和什么颜色搭配好看万维网站注册
  • Windows 10 使用 VMware Workstation 搭建 Ubuntu 虚拟机
  • 深入解析Litho的多智能体协同架构与ReAct推理机制
  • 机器视觉---ViBe算法
  • Product Hunt 每日热榜 | 2025-10-12
  • C++11 多线程与并发编程
  • 太原网站建设优化有什么ae做动图的网站
  • 【全志V821_FoxPi】9-3 Linux IIC驱动SSD1306(0.96寸oled屏幕)
  • crm管理系统登录入口官网龙华网站优化
  • 基于S32DS配置S32K344的FlexCAN模块
  • typescript中的难点总结
  • PHP 字符串处理详解
  • 【JUC】线程池有哪些拒绝策略?该如何选择使用?
  • 4 随机数 从一个随机数到另外一个随机数、等概率随机
  • 机器学习17:如何有效使用自监督式学习
  • 生成对抗网络(GAN)及其变种:CycleGAN和StarGAN
  • dede网站地图html文件公司部门撤销要求转岗不同意怎么办