hua-http-spring-boot-starter

SpringBoot 利用註解快速的編寫 HTTP 請求函數

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.hua777
ArtifactId

ArtifactId

hua-http-spring-boot-starter
Last Version

Last Version

2.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

hua-http-spring-boot-starter
SpringBoot 利用註解快速的編寫 HTTP 請求函數
Project URL

Project URL

https://github.com/Hua777
Source Code Management

Source Code Management

https://github.com/hua777/hua-http-spring-boot-starter/tree/master

Download hua-http-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.hua777/hua-http-spring-boot-starter/ -->
<dependency>
    <groupId>com.github.hua777</groupId>
    <artifactId>hua-http-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.hua777/hua-http-spring-boot-starter/
implementation 'com.github.hua777:hua-http-spring-boot-starter:2.1.1'
// https://jarcasting.com/artifacts/com.github.hua777/hua-http-spring-boot-starter/
implementation ("com.github.hua777:hua-http-spring-boot-starter:2.1.1")
'com.github.hua777:hua-http-spring-boot-starter:jar:2.1.1'
<dependency org="com.github.hua777" name="hua-http-spring-boot-starter" rev="2.1.1">
  <artifact name="hua-http-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.hua777', module='hua-http-spring-boot-starter', version='2.1.1')
)
libraryDependencies += "com.github.hua777" % "hua-http-spring-boot-starter" % "2.1.1"
[com.github.hua777/hua-http-spring-boot-starter "2.1.1"]

Dependencies

compile (8)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.3.5.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.3.5.RELEASE
org.slf4j : slf4j-api jar 2.0.0-alpha1
ch.qos.logback : logback-core jar 1.3.0-alpha5
ch.qos.logback : logback-classic jar 1.3.0-alpha5
com.alibaba : fastjson jar 1.2.73
cn.hutool : hutool-core jar 5.5.7
org.apache.httpcomponents : httpclient jar 4.5.13

Project Modules

There are no modules declared in this project.

SpringBoot 接口式註解,快速開發 HTTP 請求函數

GitHub: hua-http-spring-boot-starter

在 SpringBoot 中用註解的方式請求 Http

✏️

SpringBoot 小白的我,歡迎大家 Issues、Fork、Pull Requests 😄

POM 引入

<dependency>
    <groupId>com.github.hua777</groupId>
    <artifactId>hua-http-spring-boot-starter</artifactId>
    <version>2.0.3</version>
</dependency>

配置文件(基本可以不用配置)

com:
  github:
    hua777:
      hua-http-spring-boot-starter:
        scan-packages: xxx.xxx.xxx1,xxx.xxx.xxx2 # 自定义额外扫描类 默认启动类下的所有包
        http-timeout-seconds: 60 # 请求超时时间(秒)默认 60
        http-redirects: true # 是否自行重导向 默认 true

教學

發送 Get、Delete 請求

@HuaHttp("http://hello-world.com")
public interface TestHttp {

    /*
     * http get http://hello-world.com/get/hello/world?hello=xxx
     */
    @HuaGet(url = "/get/hello/world")
    String getHelloWorld(String hello);

    /*
     * http delete http://hello-world.com/delete/hello/world?hello=xxx
     */
    @HuaDelete(url = "/delete/hello/world")
    String deleteHelloWorld(String hello);
}

發送 Post、Put 請求

@HuaHttp("http://hello-world.com")
public interface TestHttp {
    
    /*
     * http post http://hello-world.com/post/hello/world
     * body = {
     *     "hello": "xxx"
     * }
     */
    @HuaPost(url = "/post/hello/world")
    String postHelloWorld(String hello); // default is body

    /*
     * http put http://hello-world.com/put/hello/world
     * body = {
     *     "hello": "xxx"
     * }
     */
    @HuaPut(url = "/put/hello/world")
    String putHelloWorld(String hello);
}

使用配置地址

@HuaHttp("${hello.world.url}")
public interface TestHttp {

    /*
     * http get http://hello-world.com/get/hello/world?hello=xxx
     */
    @HuaGet(url = "/get/hello/world")
    String getHelloWorld(String hello);
}

Post、Put 請求中加上 Query

@HuaHttp("http://hello-world.com")
public interface TestHttp {
    
    /*
     * http post http://hello-world.com/post/hello/world?param=xxx
     * body = {
     *     "hello": "xxx"
     * }
     */
    @HuaPost(url = "/post/hello/world")
    String postHelloWorld(String hello, @HuaQuery String param);
}

使用 Form 表單發送 Post、Put 請求

@HuaHttp("http://hello-world.com")
public interface TestHttp {
    
    /*
     * http post http://hello-world.com/post/hello/world
     * body = hello=xxx&world=xxx
     */
    @HuaPost(url = "/post/hello/world", form = true)
    String postHelloWorld(String hello, String world);
}

使用對象發送 Get、Post、Put、Delete 請求

@Data
public class YourBean {
    String hello;
    String world;
}

@HuaHttp("http://hello-world.com")
public interface TestHttp {
    
    /*
     * http post http://hello-world.com/post/hello/world?hello=xxx&world=xxx
     * body = {
     *     "hello": "xxx",
     *     "world": "xxx"
     * }
     */
    @HuaPost(url = "/post/hello/world")
    String postHelloWorld(@HuaQuery(full = true) YourBean paramBean, @HuaBody(full = true) YourBean bodyBean);
}

請求地址帶變量

@HuaHttp("http://hello-world.com")
public interface TestHttp {
    
    /*
     * http post http://hello-world.com/post/hello/world/xxx
     */
    @HuaPost(url = "/post/hello/world/{pathValue}")
    String postHelloWorld(@HuaPath String pathValue);
}

动态 Header

@HuaHeader(create = Creator.class) // 全局添加,優先級低
@HuaHttp("http://hello-world.com")
public interface TestHttp {

    /*
     * http get http://hello-world.com/get/hello/world
     * headers {
     *     "big_token1": "big_value1",
     *     "token1": "value1",
     *     "InputToken": "xxx"
     * }
     */
    @HuaGet(url = "/get/hello/world")
    @HuaHeader(create = Creator.class) // 此方法添加,優先級高
    String testHeader(@HuaHeader(name = "InputToken") String token); // 變量添加
}

自定義請求上下文

@Configuration
public class MyHandler implements HttpHandler {
    @Override
    public void beforeHttpMethod(HttpRequest request) {
        log.info("请求:" + request.toString());
    }

    @Override
    public void afterHttpMethod(HttpResponse response) {
        log.info("返回:" + response.toString());
    }

    @Override
    public String preHandleResponse(String originString) {
        // 预处理返回值
        return originString;
    }
}
@HuaAop(MyHandler.class) // 全局
@HuaHttp("http://hello-world.com")
public interface TestHttp {

    /*
     * http get http://hello-world.com/get/hello/world?hello=xxx
     */
    @HuaAop(MyHandler.class) // 只有这个函数
    @HuaGet(url = "/get/hello/world")
    Happy getHelloWorld(String hello);
}

返回流(InputStream)

@HuaHttp("http://hello-world.com")
public interface TestHttp {

    /*
     * http get http://hello-world.com/download
     */
    @HuaGet(url = "/download")
    InputStream download();

    /*
     * http get http://hello-world.com/stream
     */
    @HuaGet(
        url = "/stream", 
        streamLimit = DefaultStreamLimiter.class // 重要:请务必自定义获取数据总量的方法
        // 预设使用 response headers 内的 USER-DEFINED-DATA-COUNT 字段
    )
    Stream<XXX> stream();

}

程序内自定义额外扫描包

@Configuration
public class MyHttpHarder implements HttpHarder {
    @Override
    public Set<String> getMoreScanPackages() {
        return new HashSet<String>() {{
            add("xxx.xxx.xxx1");
            add("xxx.xxx.xxx2");
        }};
    }
}

這個項目使用的依賴包

hutool-http

用於請求 Http Hutool

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-http</artifactId>
    <version>5.5.2</version>
</dependency>

fast-json

用於解析請求返回值 Json FastJson

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.73</version>
</dependency>

Versions

Version
2.1.1
2.1.0
2.0.4
2.0.3
2.0.1
2.0.0
1.4.0
1.2.0-RELEASE
1.1.8-RELEASE
1.1.7-RELEASE
1.1.6-RELEASE
1.1.5-RELEASE
1.1.4-RELEASE
1.1.3-RELEASE
1.1.2-RELEASE
1.1.1-RELEASE
1.1.0-RELEASE
1.0.8-RELEASE
1.0.7-RELEASE
1.0.6-RELEASE
1.0.5-RELEASE
1.0.4-RELEASE
1.0.3-RELEASE
1.0.3.fix-RELEASE
1.0.2-RELEASE
1.0.1-RELEASE
1.0.0-RELEASE