java http两种方式

一、FeignClient

导包:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
   <version>1.4.7.RELEASE</version>
</dependency>

接口:

FingerprintCheck是自定义的参数实体类,根据自己需求改,返回JSONObject不必一样,Map或者String都行,根据自己需求改
package com.andin.service;

import net.sf.json.JSONObject;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * Description:
 * Author:
 * Date: 2020-12-10 17:24
 * Version: V1.0
 **/
@FeignClient(url = "${fingerprint.url}", name = "FingerprintService", fallbackFactory = FingerprintService.DefaultFallback.class)
public interface FingerprintService {

    @PostMapping(value = "/BioTrust/fingerprint/comparisonById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    JSONObject comparisonById(@RequestBody FingerprintCheck vo);


    /**
     * 容错处理类,当调用失败时,返回失败
     */
    @Component
    class DefaultFallback implements FingerprintService {

        @Override
        public JSONObject comparisonById(FingerprintCheck vo) {
            JSONObject object = new JSONObject();
            object.put("result", "0");
            object.put("message", "指纹验证接口调用失败");
            return object;
        }
    }

}

 调用:

	@Autowired
	private FingerprintService fingerprintService;

 

二、OKhttp3

导包:

<dependency>
   <groupId>com.squareup.okhttp3</groupId>
   <artifactId>okhttp</artifactId>
   <version>4.9.0</version>
</dependency>

接口:

JSONObject不一定要用net.sf.json.JSONObject,可以用alibaba的,也可以你想用啥就用啥,无所谓,自己玩;
package com.andin.utils;

import net.sf.json.JSONObject;
import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * ClassName: OkHttpUtils
 * Description:
 * date: 2020/10/16 15:10
 *
 * @author 
 */
public class OkHttpUtils {
    private static volatile OkHttpClient okHttpClient = null;

    private static final MediaType FROM_DATA = MediaType.parse("multipart/form-data");

    private OkHttpUtils(){}

    public static OkHttpClient getOkHttp(){
        if(okHttpClient ==null){
            synchronized (OkHttpUtils.class){
                if(okHttpClient == null){
                    okHttpClient = new OkHttpClient();
                }
            }
        }
        return okHttpClient;
    }

    public static String httpUpload(String url, File file,String fileName){
        RequestBody fileBody = RequestBody.create(file, MediaType.parse("application/octet-stream"));
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(FROM_DATA)
                .addFormDataPart("file", fileName, fileBody)
                .addFormDataPart("destination", "")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response;
        try {
            response = getOkHttp().newCall(request).execute();
            String jsonString = Objects.requireNonNull(response.body()).string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject httpPost(String url, String param) throws IOException {
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        Request request = new Request.Builder()
                .url(url)
                .post(RequestBody.create(JSON, param))
                .build();
        Response response = getOkHttp().newCall(request).execute();
        return JSONObject.fromObject(Objects.requireNonNull(response.body()).string());
    }

}

调用:

			JSONObject object = OkHttpUtils.httpPost(fingerprintUrl, param);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值