一、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);