相关版本:
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;