Java Rest request tools

Add dependencies

<dependency>
    <groupId>cn.gjing</groupId>
    <artifactId>tools-httpclient</artifactId>
    <version>1.2.6</version>
</dependency>
Copy the code

2. Instructions for use

The return value type is ideally the same as the target methodOtherwise, conversion exceptions may occur. This is best used when the return type is not confirmedStringTo receive. At the end of the request, you can passget()Method to get the content returnedlistener()Method specifies the listener to listen for the processing logic after the result is returned

1. No parameter request

public class Test{
    public static void main(String[] args) {
        String result = HttpClient.builder("http://127.0.0.1:8080/test", HttpMethod.GET, String.class) .execute() .get(); System.out.println(result); }}Copy the code

2. Request with request header

public class Test{
    public static void main(String[] args) {
            Map<String, String> map = new HashMap<>(16);
            map.put("head"."111");
            HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.GET, String.class) .header(map) .execute() .listener(System.out::println); }}Copy the code

3. Carry parameters

public class Test{
    public static void main(String[] args) {
        Map<String, Object> param = new HashMap<>();
        param.put("id"."1");
        HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.GET, String.class) .param(param) .execute() .listener(System.out::println); }}Copy the code

4. Carry JSON

It can be a JSON string, a Map, or a JSON entity object, which is passed through a Map

public class Test{
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("key"."code");
        map.put("val"."200");
        Map resultMap = HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.POST, Map.class) .body(map) .execute() .get(); }}Copy the code

5. Set the timeout period

Read timeout can be set separately (The default 5000 ms) and connection timeout (The default 2000 ms)

public class Test{
    public static void main(String[] args) {
        Map<String, Object> param = new HashMap<>();
        param.put("id"."1");
        HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.GET, String.class)
                .param(param)
                // Read timeout
                .readTimeout(5000)
                // Connection timeout time
                .connectTimeout(2000) .execute() .listener(System.out::println); }}Copy the code

6. Abnormal rollback

Once the request is initiated, an error will enter the fallback, which is thrown by defaultHttpException, you can define your own fallback logic for the method to be executedexecute()Call method before request, otherwise invalid. This method notifies you of an error message

public class Test{
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("key"."code");
        map.put("val"."200");
        Map resultMap = HttpClient.builder("http://127.0.0.1:8080/test6", HttpMethod.POST, Map.class)
                .body(map)
                .fallback(e -> {
                   throw new ServiceException("Ah. Back to make the"); }) .execute() .get(); }}Copy the code

UrlUtils utility class

1. Url splicing

Url concatenation. The result is returned in the format of http://xxx/param1/param2

    public static void main(String[] args) {
        String url = "http://127.0.0.1:8080/";
        Object[] param = {1.2.3.4};
        UrlUtil.urlAppend(url, param);
    }
Copy the code

2. Parameter sorting

Parameters are sorted by the Unicode code of the field name from smallest to largest (lexicographical order), resulting in the format a= parameter 1&b= parameter 2

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("a".Parameters of "1");
        map.put("b"."Parameter 2");
        UrlUtil.paramUnicodeSort(map, false.false);
    }
Copy the code

Parameters that

parameter describe
paramMap parameter
urlEncode Whether to perform URL encoding
keyToLower Whether to convert the key value of the converted parameter to lowercase

3. Transfer URL parameters to Map

Convert the parameters following the URL address to a map

    public static void main(String[] args) {
        String url = "Http://127.0.0.1:8080? a=2&b=2";
        UrlUtil.toMap(url);
    }
Copy the code

The project source code can be viewed at GitHUb:tools-httpclient