fegin返回参数统一处理

相关版本:

xml 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
    </parent>
    
    <properties>
        <spring-cloud.version>2023.0.0</spring-cloud.version>
        <spring-cloud-alibaba.version>2023.0.0.0-RC1</spring-cloud-alibaba.version>
        <fastjson2.version>2.0.50</fastjson2.version>
    </properties>

创建模块feign

引入相关依赖:

xml 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--必须与feign一起引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

        <dependency>
            <groupId>com.lease</groupId>
            <artifactId>model</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
    </dependencies>

对feign返回结果统一处理

java 复制代码
/**
 * 自定义解析类
 * @author YuelongZhou
 * @date 2024/6/22
 */
public class MyResultDecoder implements Decoder {

    private final SpringDecoder decoder;

    public MyResultDecoder(SpringDecoder springDecoder) {
        this.decoder = springDecoder;
    }


    @Override
    public Object decode(Response response, Type type) throws IOException, DecodeException, FeignException {
        Result result = JSON.parseObject(response.body().asReader(StandardCharsets.UTF_8), Result.class);
        Assert.isTrue(ResultStatusEnum.SUCCESS.getCode().equals(result.getCode()),"数据解析失败");
        String dataStr = JSON.toJSONString(result.getData());
        JavaType javaType = TypeFactory.defaultInstance().constructType(type);
        return new ObjectMapper().readValue(dataStr,javaType);
    }
}

添加feign拦截器,实现请求头上下文传递

java 复制代码
/**
 * @author YuelongZhou
 * @date 2024/6/22
 */
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String header = "access-token";
        String token = request.getHeader(header);
        requestTemplate.header(header,token);
    }
}

使上面2个配置生效

java 复制代码
@Configuration
@EnableFeignClients(basePackages = "com.lease.feign.client")
public class FeignConfig {

    @Bean
    public Decoder decoder(ObjectFactory<HttpMessageConverters> msgConverters, ObjectProvider<HttpMessageConverterCustomizer> customizers) {
        return new OptionalDecoder((new ResponseEntityDecoder(new MyResultDecoder(new SpringDecoder(msgConverters, customizers)))));
    }

    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new FeignRequestInterceptor();
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        return objectMapper;
    }

}

feign接口

java 复制代码
@FeignClient(
        value = FeignClientConstants.LEASE_WEB_ADMIN,
        path = "/admin/facility",
        contextId = "roomFacilityFeignClient"
)
public interface RoomFacilityFeignClient {

    @PostMapping("/getRoomFacilityListByRoomId")
    List<RoomFacility>getRoomFacilityListByRoomId(@RequestBody IdParam param);
}

服务controller接口

java 复制代码
    @Operation(summary = "根据房间id查询房间配套关联")
    @PostMapping("/getRoomFacilityListByRoomId")
    public Result<List<RoomFacility>>getRoomFacilityListByRoomId(@RequestBody IdParam param) {
        return ResultUtil.success(roomFacilityService.getRoomFacilityListByRoomId(param));
    }

若DTO中涉及LocalDateTime

java 复制代码
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)		// 反序列化
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    private LocalDateTime createTime;
相关推荐
txxzjmzlh9 小时前
Alibaba Seata
spring cloud
方便面不加香菜3 天前
Docker Compose 文件 -- docker-compose.yml
spring cloud·docker·容器
真上帝的左手4 天前
31. 占位-14
分布式·spring cloud·架构·系统架构
captain3766 天前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
行百里er6 天前
【Spring Insight 技术分享】如何让 AI 解释失败也不拖垮控制台
后端·spring cloud·开源
行百里er7 天前
【Spring Insight 技术分享】如何给 AI 接口调用做滑动窗口限流
spring boot·后端·spring cloud
逃逸线LOF13 天前
SpringCloud Stream
后端·spring·spring cloud
wz975415 天前
Docker 从 systemd 托管容器、overlay2 存储驱动到 Compose 多容器实战
spring cloud·docker·eureka
小郎君65820 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud
jktantan21 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构