easy-http

easy to contact http rest api

License

License

GroupId

GroupId

vip.justlive
ArtifactId

ArtifactId

easy-http
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

easy-http
easy to contact http rest api
Project URL

Project URL

http://www.justlive.vip
Source Code Management

Source Code Management

https://github.com/justlive1/easy-http

Download easy-http

How to add to project

<!-- https://jarcasting.com/artifacts/vip.justlive/easy-http/ -->
<dependency>
    <groupId>vip.justlive</groupId>
    <artifactId>easy-http</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/vip.justlive/easy-http/
implementation 'vip.justlive:easy-http:1.0.1'
// https://jarcasting.com/artifacts/vip.justlive/easy-http/
implementation ("vip.justlive:easy-http:1.0.1")
'vip.justlive:easy-http:jar:1.0.1'
<dependency org="vip.justlive" name="easy-http" rev="1.0.1">
  <artifact name="easy-http" type="jar" />
</dependency>
@Grapes(
@Grab(group='vip.justlive', module='easy-http', version='1.0.1')
)
libraryDependencies += "vip.justlive" % "easy-http" % "1.0.1"
[vip.justlive/easy-http "1.0.1"]

Dependencies

compile (4)

Group / Artifact Type Version
org.springframework : spring-web jar 4.3.5.RELEASE
com.alibaba : fastjson jar 1.2.57
vip.justlive : oxygen-core jar 2.1.2
org.slf4j : slf4j-api jar 1.7.26

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

easy-http

Maven Central License

easy to contact http rest api

介绍

  • 基于Spring mvc@RequestMapping@GetMapping@PostMapping@RequestParamRequestHeaderPathVariable等注解扩展
  • 自动扫描接口实例化并托管至Spring,参考自Mybatis自动扫描Mapper
  • 需要声明的接口和Server端保持一致即可完美对接

快速开始

引入

创建Maven项目

<dependency>
    <groupId>vip.justlive</groupId>
    <artifactId>easy-http</artifactId>
    <version>${lastVersion}</version>
</dependency>

Gradle

compile 'vip.justlive:easy-http:$lastVersion'

使用方式

// 创建接口

@HttpClient
@RequestMapping("https://api.github.com")
public interface GithubApi {

  @RequestMapping(method = RequestMethod.GET)
  String root();


  @GetMapping("/repos/{owner}/{repo}")
  Repository repos(@PathVariable String owner, @PathVariable String repo);

  @Data
  class Repository {

    private Long id;
    private String name;
    private int forks;
    private int watchers;
  }
}

// 使用${xx}获取配置
@HttpClient
@RequestMapping("${thirdpart.api.url}")
public interface ThirdpartApi {

  @PostMapping("/api/token")
  RespVo<AccessToken> token(@RequestParam String appKey, @RequestParam String appSecret);

  @PostMapping("/api/account")
  RespVo<Account> account(@RequestHeader String accessToken, @RequestBody Account account);

  @Data
  @Accessors(chain = true)
  class AccessToken {

    private String accessToken;
    private Long expiresIn;
  }

  @Data
  @Accessors(chain = true)
  class Account {

    private String name;
    private BigDecimal balance;
  }
}

// 增加扫描(默认为当前类所处包)

@HttpClientScan("com.xxx")
@Configuration
public class HttpClientAutoConfiguration {

}

// 使用

@Slf4j
@Component
public class Demo {

  @Autowired
  private GithubApi githubApi;

  @Autowired
  private ThirdpartApi thirdpartApi;

  @PostConstruct
  private void init() {
   
    String result = githubApi.root();
    log.info("githubApi root {}", result);

    Repository repository = githubApi.repos("justlive1", "easy-http");
    log.info("githubApi repos {}", repository);
    
    AccessToken token = thirdpartApi.token("key", "secret").getData();
    log.info("thirdpartApi token {}", token);

    Account account = new Account().setName("aa").setBalance(new BigDecimal("12.3"));
    account = thirdpartApi.account(token.getAccessToken(), account).getData();
    log.info("thirdpartApi account {}", account);
  }
}

Versions

Version
1.0.1
1.0.0