我是靠谱客的博主 小巧刺猬,最近开发中收集的这篇文章主要介绍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);

 

最后

以上就是小巧刺猬为你收集整理的java http两种方式的全部内容,希望文章能够帮你解决java http两种方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部