How to custom QueryMapEncoder when using annotation @QueryMap to construct a query in Feign

当使用Feign调用下游服务时,可能需要在URL中传递多个查询参数。如果参数较少,可以使用@Param注解传递。但参数多时,代码会变得复杂。一种解决方案是使用@QueryMap从映射中构造查询参数,但这种方式不够友好且可读性差。另一种更好的方式是通过QueryMapEncoder从POJO对象生成查询参数,提高代码的可读性和组织性。
部署运行你感兴趣的模型镜像

When we use Feign to call downstream services, we may need to pass several query parameters in URL. If there are just one or two query parameters, we can use annotation @Param to pass the value of the quey parameters.

@RequestLine("GET /repos?owner={owner}&repo={repo}")
List<Contributor> getContributors(@Param("owner") String owner, @Param("repo") String repo);

But if there are many query parameters, the code will look a little complicated.

@RequestLine("GET /repos?owner={owner}&repo={repo}&branches={branches}&commitStartDate={commitStartDate}&commitEndDate={commitEndDate}")
List<Contributor> getContributors(@Param("owner") String owner,
                                  @Param("repo") String repo,
                                  @Param("branches") String branches,
                                  @Param("commitStartDate") String commitStartDate,
                                  @Param("commitEndDate") String commitEndDate);

One solution is to use the annotation @QueryMap to construct a query that uses the contents of the map as its query parameters. But it is not so friendly and not readable.

@RequestLine("GET /repos")
List<Contributor> getContributors(@QueryMap Map<String, Object> queryMap);

Another solution is to generate the query parameters from a POJO object using a QueryMapEncoder.

@RequestLine("GET /repos")
List<Contributor> getContributors(@QueryMap GitHub gitHub);
public class GitHub
{
    private String owner;
    private String repository;
    private List<String> branchs;
    private Date commitStartDate;
    private Date commitEndDate;

    public GitHub(String owner, String repository, List<String> branchs, Date commitStartDate, Date commitEndDate) {
        this.owner = owner;
        this.repository = repository;
        this.branchs = branchs;
        this.commitStartDate = commitStartDate;
        this.commitEndDate = commitEndDate;
    }
}

Reference:

https://github.com/OpenFeign/feign

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

In a context like CSM (Component Service Manager) in a product like DaVinci, configuring asynchronous jobs typically involves setting up background tasks or services that run independently of the main application flow. Here's a high-level overview: 1. **Create Task Definition**: Define an asynchronous task by creating a service component with attributes specifying its execution as non-blocking or异步的 (e.g., using `async` or `Background` flag). ```xml <service> <name>MyAsyncTask</name> <type>com.example.MyAsyncTaskService</type> <async>true</async> </service> ``` 2. **Implement Service Component**: Implement the component class with a method annotated for asynchronous execution. Use frameworks like Spring (Spring @Async annotation), or implement your own thread management if needed. ```java @Service public class MyAsyncTaskService { @Async public void executeJob(JobRequest request) { // Asynchronous job logic here } } ``` 3. **Queue Management**: If you want to manage a queue for these tasks, you can use a message queue like RabbitMQ, Kafka, or built-in messaging systems provided by DaVinci. Configure the service to send tasks to the queue and listen for responses. 4. **Error Handling**: Include error handling mechanisms to handle exceptions and ensure proper logging or notifications when a task fails. 5. **Monitoring & Scheduling**: Use monitoring tools and scheduling features within the CSM to track progress, set schedules, and trigger async jobs at specific intervals.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值