# myhttp
**Repository Path**: pangyajun123/myhttp
## Basic Information
- **Project Name**: myhttp
- **Description**: 基于http协议api调用框架
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 0
- **Created**: 2021-12-11
- **Last Updated**: 2022-11-06
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# myhttp
#### 介绍
基于http协议api调用框架
#### 快速开始
基于springboot
##### 第一步:添加maven依赖
添加maven依赖
```xml
com.iwbfly
myhttp-spring-boot-starter
1.0.0
```
##### 第二步:创建interfate
高德地图api
```java
package com.iwbfly.client;
import com.iwbfly.myhttp.annotation.Get;
import com.iwbfly.myhttp.annotation.MyhttpClient;
import java.util.Map;
@MyhttpClient(url = "http://ditu.amap.com",timeout = 300)
public interface AmapClient {
/**
* @Get注解代表该方法专做GET请求
* 在url中的${0}代表引用第一个参数,${1}引用第二个参数
*/
@Get(url = "service/regeo?longitude=${0}&latitude=${1}")
Map getLocation(String longitude, String latitude);
}
```
##### 第三步:扫描接口
在Spring Boot的配置类或者启动类上加上@EnableMyhttpClients注解,并在属性里填上远程接口的所在的包名
```java
package com.iwbfly;
import com.iwbfly.myhttp.EnableMyhttpClients;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableMyhttpClients("com.iwbfly.client")
public class MyhttpApplication {
public static void main(String[] args) {
SpringApplication.run(MyhttpApplication.class);
}
}
```
##### 第四步:调用接口
接下来就可以愉快的调用接口了
```java
package com.iwbfly;
import com.iwbfly.client.AmapClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Map;
@SpringBootTest
class MyhttpApplicationTests {
@Autowired
private AmapClient amapClient;
@Test
void getLocation() {
Map result = amapClient.getLocation("121.475078", "31.223577");
System.out.println(result);
}
}
```
#### yml参数配置
```yml
myhttp:
okhttp:
enabled: true
httpcleint:
maxConnections: 10
cleint:
timeout: 3000 #超时时间
retry-count: 3 #失败重试次数
max-retry-interval: 600 #重试间隔时间
variables: #模板参数
localhostUrl: http://localhost:8080
amapUrl: http://ditu.amap.com
```
#### get请求
```java
package com.iwbfly.http.client;
import com.iwbfly.model.User;
import com.iwbfly.myhttp.annotation.*;
import com.iwbfly.myhttp.http.Response;
import com.iwbfly.myhttp.logging.Logger;
import java.util.Map;
/**
* {@code @Query}参数拼接在url后面
*
**/
@MyhttpClient(url = "${localhostUrl}",
headers = {
"Accept-Charset: UTF-8",
"Accept: text/plain"
}
)
public interface GetClient {
@Get(url = "test?username=${0}&password=${1}",
headers = {"token:6666666"},
loggerLevel = Logger.Level.BASIC)
String get1(String username, String password);
@Get(url = "test",
headers = {"token1:${0}", "token2:${1}"},
loggerLevel = Logger.Level.FULL)
String get2(@Query("username") String username,
@Query("password") String password,
@Header("token") String token);
@Get(url = "test",
headers = {"token:666666"},
data={"data1=${$0.username}&data2=${user.password}"})
String get3(@Query @Variable("user") User user);
@Get(url = "test", headers = {"token:666666"})
String get4(@Query User user);
@Get(url = "test?username=${user.username}&password=${user.password}",
headers = {"token:${user.username}"})
String get5(@Variable("user") User user);
@Get(url = "test", headers = {"token:666666"})
Response get6(@Query User user);
}
```
#### post请求
```java
package com.iwbfly.http.client;
import com.iwbfly.model.User;
import com.iwbfly.myhttp.annotation.*;
import com.iwbfly.myhttp.logging.Logger;
import java.util.List;
/**
* {@code @Body} 作为http body里面的数据提交,post请求生效,get请求无效
*
**/
@MyhttpClient(url = "${localhostUrl}",
headers = {"Accept-Charset: UTF-8",
"Accept: text/plain"},
contentType = "application/json"
)
public interface PostClient {
@Post(url = "test")
String post1(@Body("username") String username, @Body("password") String password);
@Post(url = "test",
data = "username=${$0.username}&password=${$0.password}")
String post2(User user);
@Post(url = "test")
String post3(@Body List userList, @Header("token") String token);
@Post(url = "test",
data = "{\"username\":\"${$0.username}\",\"password\":\"${$0.password}\"}"
)
String post4(User user);
@Post(url = "test",
loggerLevel = Logger.Level.BASIC,
headers = {"token:666666"})
String post5(@Body User user);
@Post(url = "test",
contentType = "application/x-www-form-urlencoded",
loggerLevel = Logger.Level.BASIC,
data = "username=${$0}&password=${1}"
)
String post6(String username, String password);
@Post(url = "test",
data = "{\"username\":\"${user.username}\",\"password\":\"${user.password}\"}",
headers = {"token1:${1}","token2:${user.password}"}
)
String post7(@Variable("user") User user,String token1,@Header("token3")String token3);
}
```
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request