当前位置: 移动技术网 > IT编程>开发语言>Java > Java调用Http接口(7,end)--WebClient调用Http接口

Java调用Http接口(7,end)--WebClient调用Http接口

2019年11月30日  | 移动技术网IT编程  | 我要评论

webclient是spring提供的非阻塞、响应式的http客户端,提供同步及异步的api,将会代替resttemplate及asyncresttemplate。文中所使用到的软件版本:java 1.8.0_191、springboot 2.2.1.release。

1、服务端

参见java调用http接口(1)--编写服务端 

2、调用

使用webclient需要用到reactor netty,依赖如下:

        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webflux</artifactid>
        </dependency>
        <dependency>
            <groupid>io.projectreactor.netty</groupid>
            <artifactid>reactor-netty</artifactid>
        </dependency>

2.1、get请求

    public static void get() {
        string requestpath = "http://localhost:8080/demo/httptest/getuser?userid=1000&username=李白";
        webclient webclient = webclient.create();
        mono<string> mono = webclient.get().uri(requestpath).retrieve().bodytomono(string.class);
        //同步方式
        system.out.println("get block返回结果:" + mono.block());
        
        //异步方式
        final countdownlatch latch = new countdownlatch(5);
        for (int i = 0; i < 5; i++) {
            requestpath = "http://localhost:8080/demo/httptest/getuser?userid=1000&username=李白" + i;
            mono = webclient.get().uri(requestpath).retrieve().bodytomono(string.class);
            mono.subscribe(new consumer<string>() {
                @override
                public void accept(string s) {
                    latch.countdown();
                    system.out.println("get subscribe返回结果:" + s);
                }
            });
        }
        
        try {
            latch.await();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

2.2、post请求(发送键值对数据)

    public static void post() {
        string requestpath = "http://localhost:8080/demo/httptest/getuser";
        webclient webclient = webclient.create();
        multivaluemap<string, string> map = new linkedmultivaluemap <string, string>();
        map.add("userid", "1000");
        map.add("username", "李白");
        mono<string> mono = webclient.post().uri(requestpath).bodyvalue(map).retrieve().bodytomono(string.class);
        system.out.println("post返回结果:" + mono.block());
    }

2.3、post请求(发送json数据)

    public static void post2() {
        string requestpath = "http://localhost:8080/demo/httptest/adduser";
        webclient webclient = webclient.create();
        string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}";
        mono<string> mono = webclient.post().uri(requestpath).contenttype(mediatype.application_json).bodyvalue(param)
                .retrieve().bodytomono(string.class);
        system.out.println("post json返回结果:" + mono.block());
    }

2.4、上传文件

    public static void upload() {
        string requestpath = "http://localhost:8080/demo/httptest/upload";
        webclient webclient = webclient.create();
        mono<string> mono = webclient.post().uri(requestpath).contenttype(mediatype.application_octet_stream)
                .bodyvalue(new filesystemresource("d:/a.jpg")).retrieve().bodytomono(string.class);
        system.out.println("upload返回结果:" + mono.block());
    }

2.5、上传文件及发送键值对数据

    public static void mulit() {
        string requestpath = "http://localhost:8080/demo/httptest/multi";
        webclient webclient = webclient.create();
        
        multipartbodybuilder builder = new multipartbodybuilder();
        builder.part("param1", "参数1");
        builder.part("param2", "参数2");
        builder.part("file", new filesystemresource("d:/a.jpg"));
        multivaluemap<string, httpentity<?>> parts = builder.build();
        mono<string> mono = webclient.post().uri(requestpath)
                .bodyvalue(parts).retrieve().bodytomono(string.class);
        system.out.println("mulit返回结果:" + mono.block());
    }

2.6、完整例子

package com.inspur.demo.http.client;

import java.util.concurrent.countdownlatch;
import java.util.function.consumer;

import org.springframework.core.io.filesystemresource;
import org.springframework.http.httpentity;
import org.springframework.http.mediatype;
import org.springframework.http.client.multipartbodybuilder;
import org.springframework.util.linkedmultivaluemap;
import org.springframework.util.multivaluemap;
import org.springframework.web.reactive.function.client.webclient;

import reactor.core.publisher.mono;

/**
 * 
 * 通过webclient调用http接口
 *
 */
public class webclientcase {
    /**
     *  get请求
     */
    public static void get() {
        string requestpath = "http://localhost:8080/demo/httptest/getuser?userid=1000&username=李白";
        webclient webclient = webclient.create();
        mono<string> mono = webclient.get().uri(requestpath).retrieve().bodytomono(string.class);
        //同步方式
        system.out.println("get block返回结果:" + mono.block());
        
        //异步方式
        final countdownlatch latch = new countdownlatch(5);
        for (int i = 0; i < 5; i++) {
            requestpath = "http://localhost:8080/demo/httptest/getuser?userid=1000&username=李白" + i;
            mono = webclient.get().uri(requestpath).retrieve().bodytomono(string.class);
            mono.subscribe(new consumer<string>() {
                @override
                public void accept(string s) {
                    latch.countdown();
                    system.out.println("get subscribe返回结果:" + s);
                }
            });
        }
        
        try {
            latch.await();
        } catch (exception e) {
            e.printstacktrace();
        }
    }
    
    /**
     *  post请求(发送键值对数据)
     */
    public static void post() {
        string requestpath = "http://localhost:8080/demo/httptest/getuser";
        webclient webclient = webclient.create();
        multivaluemap<string, string> map = new linkedmultivaluemap <string, string>();
        map.add("userid", "1000");
        map.add("username", "李白");
        mono<string> mono = webclient.post().uri(requestpath).bodyvalue(map).retrieve().bodytomono(string.class);
        system.out.println("post返回结果:" + mono.block());
    }
    
    /**
     *  post请求(发送json数据)
     */
    public static void post2() {
        string requestpath = "http://localhost:8080/demo/httptest/adduser";
        webclient webclient = webclient.create();
        string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}";
        mono<string> mono = webclient.post().uri(requestpath).contenttype(mediatype.application_json).bodyvalue(param)
                .retrieve().bodytomono(string.class);
        system.out.println("post json返回结果:" + mono.block());
    }
    
    /**
     * 上传文件
     */
    public static void upload() {
        string requestpath = "http://localhost:8080/demo/httptest/upload";
        webclient webclient = webclient.create();
        mono<string> mono = webclient.post().uri(requestpath).contenttype(mediatype.application_octet_stream)
                .bodyvalue(new filesystemresource("d:/a.jpg")).retrieve().bodytomono(string.class);
        system.out.println("upload返回结果:" + mono.block());
    }
    
    /**
     * 上传文件及发送键值对数据
     */
    public static void mulit() {
        string requestpath = "http://localhost:8080/demo/httptest/multi";
        webclient webclient = webclient.create();
        
        multipartbodybuilder builder = new multipartbodybuilder();
        builder.part("param1", "参数1");
        builder.part("param2", "参数2");
        builder.part("file", new filesystemresource("d:/a.jpg"));
        multivaluemap<string, httpentity<?>> parts = builder.build();
        mono<string> mono = webclient.post().uri(requestpath)
                .bodyvalue(parts).retrieve().bodytomono(string.class);
        system.out.println("mulit返回结果:" + mono.block());
    }
    
    public static void main(string[] args) {
        get();
        post();
        post2();
        upload();
        mulit();
    }

}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网