Giter Club home page Giter Club logo

ihttp's Introduction

ihttp - 轻量级HTTP客户端框架

ihttp 使用注解方式配置Java网络编程,避免大量编写构造请求的代码,更优雅的编码。

Maven Central License

快速开始

<dependency>
    <groupId>com.github.zhanjixun</groupId>
    <artifactId>ihttp</artifactId>
    <version>19.10</version>
</dependency>

发送一个GET请求

//就像mybatis一样定义一个接口映射http请求
public interface Mapper {
    
    @GET
    @URL("https://github.com/")
    Response openBaidu();
    
}

//调用
Mapper mapper = IHTTP.getMapper(Mapper.class);
Response response = mapper.openBaidu();
//获取Response对象进行返回结果处理

目前支持的http请求方法有:@GET、@POST、@PUT、@DELETE

携带请求参数

//请求参数:用于方法体或方法参数上
@Param(name = "userName", value = "zhanjixun")

//请求参数:用于方法体上,自动生成时间戳请求参数
@TimestampParam(name = "_", unit = TimeUnit.MILLISECONDS)

//请求参数:用于方法体上,自动生成随机字符串
@RandomParam(name = "key", length = 6, chars = "0123456789")

携带请求头

//请求头:用于方法体或方法参数上
@Header(name = "User-Agent", value = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36")

//语义化请求注解:用于方法体或方法参数上,携带相关请求头
@Accept
@AcceptEncoding
@AcceptLanguage
@Origin
@Referer
@UserAgent

携带请求体

//请求体:用于方法体后方法参数上
@StringBody("{\"cat\":{\"name\":\"Matilda\"}}")

//请求体:用于方法参数上,将对象直接转成json作为请求体发送
@StringBodyObject

使用IHTTP就是这么简单,不需要关注如何构建和发送HTTP请求,只需要几个注解本框架帮你通通搞定这些,让你可以专注于接口返回的处理。

关于Cookie的使用

IHTTP中默认开启为每个Mapper启用Cookie,Mapper接口继承CookiesStore将获取Cookie操作的能力。如下:

interface Mapper extends CookiesStore {
    @GET
    @URL("https://github.com/")
    Response openBaidu();
}

Mapper mapper = IHTTP.getMapper(Mapper.class);
Response response = mapper.openBaidu();
mapper.getCookies().forEach(System.out::println);

在Spring中使用

在上面的示例中都是直接使用IHTTP.getMapper(Mapper.class);来获取Mapper实例,如果是在Spring环境中使用,可以使用Spring对象容器来托管Mapper。只需要在spring-context.xml中添加扫描bean,然后就可以注入实例使用。

<!--扫描ittp映射接口-->
<bean class="com.zhanjixun.ihttp.spring.MapperScanner">
    <!--mapper接口所在包-->
    <property name="basePackage" value="com.zhanjixun.ihttp.test"/>
</bean>
//在需要使用的地方注入Mapper
@Autowired
private Mapper mapper;

ihttp's People

Contributors

zhanjixun avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.