grace-api一款http请求代理框架

Grace-API是一个基于Hutool的HTTP请求代理框架,旨在减少重复的HTTP请求代码,提高开发效率。它支持GET、POST方法,文件上传,自定义拦截器,自动签名,缓存,序列化,异常降级和日志配置。开发者可以通过注解轻松定义HTTP客户端接口并进行业务调用。
摘要由CSDN通过智能技术生成

grace-api

简介:一款http请求代理框架,能够将重复冗余,不好管理的http请求代码简化,提升开发效率。

特性:
1.以hutool http为后端框架
2.通过调用java接口的方式去发送Http请求, 实现了业务逻辑与Http协议之间的解耦
3.支持请求方法:GET, POST
4.支持文件上传
5.支持自定义拦截器
6.支持自动签名处理
7.支持缓存
8.支持自动序列化
9.支持异常降级处理
10.支持全局或者单个客户端端日志级别设置以及超时配置。

快速使用:

1.在pom.xml里面配置仓库地址

  <repositories>
    <repository>
      <id>grace-project</id>
      <url>https://gitee.com/graceful-code/maven/raw/master</url>
    </repository>
  </repositories>

2.引用依赖坐标

<dependency>
    <groupId>com.grace</groupId>
    <artifactId>grace-api</artifactId>
    <version>1.0.0-RELEASE</version>
</dependency>

3.使用注解 @EnableGraceHttpProxyClients 开启http代理框架

@SpringBootApplication
@EnableGraceHttpProxyClients
public class GraceTestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GraceTestServiceApplication.class, args);
    }

}

4.定义http代理客户端接口

@GraceHttpClient(value = "test", fallback = HelloApiFallBack.class)
public interface HelloApi {

    @GraceRequest(value = "/test", method = RequestMethod.POST)
    Result hello(@RequestParam Integer code);
}

5.配置客户端IP

grace:
  api:
    client:
      test:
        endpoint: http://127.0.0.1:8088
        timeout: 3000

这个test要和接口中 @GraceHttpClient注解里的value值对应起来,多个客户端接口可以使用相同的value,也可以每个客户端都不同。
6.业务调用

    @Resource
    private HelloApi helloApi;
    
    @GetMapping("/hello")
    public Result test(@RequestParam Integer code){
        return helloApi.hello(code);
    }

7.放弃原来复杂冗余并且不好维护http调用方式

String json = JSONObject.toJsonString(user);
HttpResponse httpResponse = HttpRequest.post("http://localhost:8088/api//user")
    .body(json)
    .timeout(3000)
    .execute();
if (httpResponse.isOk()){
    String res = httpResponse.body();
    return JSONObject.parseObject(res, Result.class);
}
return Result.builder().code(500).build();
更多高级功能文档待后续添加。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值