Feign Client踩坑日记

A项目远程调用另一项目时是用http方式进行请求的,使用的工具是FeignClient, 以下是FeignClient的踩坑日记
1.在服务的启动入口,即main方法所在的类上需要加一个EnableFeignClients注解,并且如果需要扫描的包不在该类所属的同一包下,需要加上basePackages,否则可能会生成一个代理对象,并且报错

1
Invalid bound statement (not found): xxx.common.xx.xx.xxXClient.getXxxList

解决方法如下:

1
2
3
4
5
6
7
8
@EnableFeignClients(basePackages = {"test.common"})
public class Main {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Main.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}

阅读更多

Java中枚举类的序列化与反序列化

在Spring Boot项目中,常常会遇到实体类的某个字段是枚举类,但是数据库/前端 使用的是枚举类的code,所以会涉及到Json 序列化与反序列化的需求,这里以一个例子来记录如何正确地使用Json注解。

项目中使用的Json工具是Jackson,以该工具为例

1. 实体类定义

1
2
3
4
5
6
7
8
9
10
public class Transaction {

@Schema(description = "Transaction ID")
@TableId(value = "id")
private Long id;

@Schema(description = "交易类型")
@JsonAlias({"transaction_type", "type"})
private TransactionType type;
}

阅读更多