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;
相关推荐
Wx-bishekaifayuan1 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
Stringzhua3 小时前
【SpringCloud】Kafka消息中间件
spring·spring cloud·kafka
想进大厂的小王8 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
customer089 小时前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
杨荧9 小时前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
aloha_7891 天前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
茶馆大橘1 天前
微服务系列五:避免雪崩问题的限流、隔离、熔断措施
java·jmeter·spring cloud·微服务·云原生·架构·sentinel
荆州克莱1 天前
[FE] React 初窥门径(四):React 组件的加载过程(render 阶段)
spring boot·spring·spring cloud·css3·技术