easy-http

Easy request for http and https.

License

License

GroupId

GroupId

com.github.cnzhoutao
ArtifactId

ArtifactId

easy-http
Last Version

Last Version

0.0.2-RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

easy-http
Easy request for http and https.
Project URL

Project URL

https://github.com/cnzhoutao/easy-http
Source Code Management

Source Code Management

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

Download easy-http

How to add to project

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

Dependencies

compile (7)

Group / Artifact Type Version
org.springframework : spring-context jar 5.1.5.RELEASE
org.springframework : spring-webmvc Optional jar 5.1.5.RELEASE
com.alibaba : fastjson jar 1.2.56
org.apache.httpcomponents : httpclient jar 4.5.9
org.apache.commons : commons-lang3 jar 3.8.1
org.json : json jar 20180813
org.apache.commons : commons-collections4 jar 4.4

Project Modules

There are no modules declared in this project.

1. easy-http 是什么

是否还在为如何写http/https请求而烦恼。是否有想过能有一个包能帮你屏蔽掉一切细节,让你能够专心的应付自己的业务逻辑 那么easy-http正是为了帮你解决这些问题而出现的。
你只需要简单的定义一个接口,标注一下请求方式,就能帮你实现网络请求了。

2. 如何使用

看个简单的例子:

2.1 get请求

  1. 启动类似加@EnableApi(basePackages = {"com.github.cnzhoutao.api"}) 注解,并标明定义的api所在的路径
  2. 定义接口,接口上申明需要请求的接口的信息,不需要实现
import com.github.easy_http.annotation.Api;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author zt
 * @Date: 2021/2/10 10:30 上午
 */
@Api(url = "", hostName = "www.jianshu.com", https = true)
public interface JianShuApi {

    @GetMapping(value = "/")
    String indexPage();
}
  1. 调用
package com.github.cnzhoutao;

import com.github.cnzhoutao.api.JianShuApi;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class EasyHttpTestApplicationTests {

    @Resource
    private JianShuApi jianShuApi;

    @Test
    public void testJianshu() {
        String s = jianShuApi.indexPage();
        System.out.println(s);
    }

}
  1. 返回结果

    image-20210210122538090

2.2 post请求

  1. 先来个controller吧,实现一个简单的登录功能

    package com.github.cnzhoutao.controller;
    
    import com.github.cnzhoutao.dto.UserDTO;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zt
     * @Date: 2021/2/10 12:04 下午
     */
    @RestController
    @RequestMapping(value = "/test")
    public class LoginController {
    
        @PostMapping(value = "/login")
        public String login(@RequestBody UserDTO userDTO) {
            System.out.println("接收到登录请求:用户名:" + userDTO.getUserName() + "----密码:" + userDTO.getPwd());
            return "{\"res\":\"登录成功\"}";
    
        }
    }
  2. UserDTO

    package com.github.cnzhoutao.dto;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    import java.io.Serializable;
    
    /**
     * @author zt
     * @Date: 2021/2/10 12:05 下午
     */
    @Data
    @Accessors(chain = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public class UserDTO implements Serializable {
    
        //用户名
        private String userName;
    
        //密码
        private String pwd;
    
    }
  3. api接口定义

    package com.github.cnzhoutao.api;
    
    import com.github.cnzhoutao.dto.UserDTO;
    import com.github.easy_http.annotation.Api;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    
    /**
     * @author zt
     * @Date: 2021/2/10 12:08 下午
     */
    @Api(url = "/test", hostName = "127.0.0.1:8080", https = false)
    public interface LoginApi {
    
        @PostMapping(value = "/login")
        String login(@RequestBody UserDTO userDTO);
    
    }
  4. 单元测试

    package com.github.cnzhoutao;
    
    import com.github.cnzhoutao.api.JianShuApi;
    import com.github.cnzhoutao.api.LoginApi;
    import com.github.cnzhoutao.dto.UserDTO;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import javax.annotation.Resource;
    
    @SpringBootTest
    class EasyHttpTestApplicationTests {
    
        @Resource
        private JianShuApi jianShuApi;
    
        @Resource
        private LoginApi loginApi;
    
        @Test
        public void testJianshu() {
            String s = jianShuApi.indexPage();
            System.out.println(s);
        }
    
        @Test
        public void testLogin(){
            UserDTO userDTO=new UserDTO("cnzhoutao","github");
            String result = loginApi.login(userDTO);
            System.out.println(result);
        }
    
    }
  5. 返回结果

    image-20210210124535800

image-20210210124603954

3. header和cookie如何添加

很简单,只需要在需要操作的接口上添加,@CookieHandler 或@HeaderHandler 注解就好了

image-20210210125029824

CookieHandler和HeaderHandler会规格自提供一个map,只需要将想要添加的header或cookie放入相应的map即可

image-20210210125252849

特别注意:因为cookie和header是存放在ThreadLocal中,在api使用结束时,需要手动清理

image-20210210125423365

测试项目地址

https://github.com/cnzhoutao/easy-http-test

依赖

        <dependency>
            <groupId>com.github.cnzhoutao</groupId>
            <artifactId>easy-http</artifactId>
            <version> ${latest.version} </version>
        </dependency>

Versions

Version
0.0.2-RELEASE
0.0.1-RELEASE