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;
相关推荐
荆州克莱1 天前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
解梦者1 天前
Spring(七)Spring Cloud----Feign、Zuul和Apollo
spring·spring cloud·feign·apollo·zuul
新手小袁_J2 天前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
维李设论2 天前
Node.js的Web服务在Nacos中的实践
前端·spring cloud·微服务·eureka·nacos·node.js·express
Doker 多克3 天前
IntelliJ IDEA Docker集成
spring cloud·docker·intellij-idea
Hello Dam3 天前
面向微服务的Spring Cloud Gateway的集成解决方案:用户登录认证与访问控制
spring cloud·微服务·云原生·架构·gateway·登录验证·单点登录
小马爱打代码3 天前
SpringCloud(注册中心+OpenFeign+网关+配置中心+服务保护+分布式事务)
分布式·spring·spring cloud
小笨猪-3 天前
统⼀服务⼊⼝-Gateway
java·spring cloud·微服务·gateway
岁月变迁呀3 天前
Spring Cloud Gateway 源码
java·spring·spring cloud·gateway